小编Tom*_*myD的帖子

为什么使用 SFINAE 查找方法是否存在会失败并使用 std::vector::begin

我正在寻找一种方法来检测模板类是否具有方法begin,endresize.

我尝试了这个答案的修改版本:

#include <iostream>
#include <vector>

// SFINAE test
template <typename T>
class has_method
{
    typedef char one;
    struct two { char x[2]; };

    template <typename C> static one test( decltype(&C::begin) ) ;
    template <typename C> static two test(...);    

public:
    enum { value = sizeof(test<T>(0)) == sizeof(char) };
};

int main(int argc, char *argv[])
{
    std::cout << has_method<std::vector<int>>::value << std::endl;
    
    return 0;
}
Run Code Online (Sandbox Code Playgroud)

然而,这会打印 0。有趣的是,这将适用于cbeginandcend但不适用于begin, end …

c++ sfinae

25
推荐指数
1
解决办法
964
查看次数

标签 统计

c++ ×1

sfinae ×1