我正在寻找一种方法来检测模板类是否具有方法begin,end和resize.
我尝试了这个答案的修改版本:
#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 …