Re: about SFINAE usage
Noah Roberts wrote:
feverzsj wrote:
i wrote a template to test whether a type is a class type.
but i find that using an ordinary member function "test()" will cause
compiler error like"no qualified name for pointer to member".
what's the difference in this place???
template<typename T>
class ClassChecker{
typedef char One;
typedef struct{char _c[2];} Two;
template<typename C> static One test(int C::*);
template<typename C> static Two test(...);
public:
enum{YES = sizeof(test<T>(0))==1};
enum{NO = !YES};
};
template<typename T>
void CheckClass()
{
if( ClassChecker<T>::YES )
cout<<"type '"<<typeid(T).name()<<"' IS a Class type"<<endl;
else
cout<<"type '"<<typeid(T).name()<<"' IS NOT a Class type"<<endl;
}
You really do need to supply the calling code because the above worked
just fine for me once I supplied a couple classes and called CheckClass():
struct X
{
int f() { return 5; }
};
struct Y { void f() {} };
struct Z {};
union WTF { int x; double z; };
int main()
{
CheckClass<Y>();
CheckClass<X>();
CheckClass<Z>();
CheckClass<int>();
CheckClass<WTF>();
cin.get();
}
type 'struct Y' IS a Class type
type 'struct X' IS a Class type
type 'struct Z' IS a Class type
type 'int' IS NOT a Class type
type 'union WTF' IS a Class type
Note the last bit there. It seems that the code is actually totally
inadequate for telling if a type is a class or not.
Why do you say that it is inadequate? Is union not a class? Can it not
have members? Can it not have the address of a member taken and
converted to a pointer-to-member? What *make* a class, in your opinion?
Does it have to be defined/declared with the keyword 'class'? For
references, see 9/1.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask