iam*_*ind 6 c++ templates compiler-errors sfinae
带着好奇心,我正在尝试使用这个技巧替代实现is_class构造sizeof().以下是代码:
template<typename T>
struct is_class
{
typedef char (&yes)[7];
typedef char (&no)[3];
static yes check (int T::*);
static no check (...);
enum { value = (sizeof(check(0)) == sizeof(yes)) };
};
Run Code Online (Sandbox Code Playgroud)
问题是当我实例化时is_class<int>,它给出了编译错误:
error: creating pointer to member of non-class type ‘int’
Run Code Online (Sandbox Code Playgroud)
现在,我的问题是,如果int T::*不适用int(或void*等),那么为什么不替代失败的yes check.不应该编译选择no check?
GMa*_*ckG 10
yes并且no不是模板,SFINAE不可能适用于它们.你需要这样做:
template<typename T>
struct is_class
{
typedef char (&yes)[7];
typedef char (&no)[3];
template <typename U>
static yes check (int U::*);
template <typename>
static no check (...);
enum { value = (sizeof(check<T>(0)) == sizeof(yes)) };
};
Run Code Online (Sandbox Code Playgroud)
现在SFINAE可以加入.