我正在尝试应用中描述的技术
http://www.drdobbs.com/tools/227500449
对于下面的示例代码,我期望输出:
1 0 0 0
0 1 0 0
0 0 1 0
0 0 0 1
如果我使用clang编译,这确实会发生。但对于GCC,此代码给出以下错误:
junk.cpp: In instantiation of âconst bool has_foo_member_variable<B>::valueâ:
junk.cpp:45:5: instantiated from âvoid print() [with T = B]â
junk.cpp:82:14: instantiated from here
junk.cpp:30:75: error: âB::fooâ is not a valid template argument for type âint B::*â
junk.cpp:30:75: error: it must be a pointer-to-member of the form `&X::Y'
junk.cpp: In instantiation of âconst bool has_foo_member_variable<D>::valueâ:
junk.cpp:45:5: instantiated from âvoid print() [with T = D]â
junk.cpp:84:14: instantiated from here
junk.cpp:30:75: error: â& D::fooâ is not a valid template argument for type âint D::*â
junk.cpp:30:75: error: it must be a pointer-to-member of the form `&X::Y'
我使用GCC4.5.1……我看起来GCC没有遵循正确的sfinae规则,但我不是100%确定。clang正确吗?这是gcc错误?
#include <iostream>
struct small_type { char dummy; };
struct large_type { char dummy[2]; };
template<class T>
struct has_foo_member_function
{
template<int (T::*)()> struct tester;
template<class U> static small_type has_foo(tester<&U::foo> *);
template<class U> static large_type has_foo(...);
static const bool value = (sizeof(has_foo<T>(0)) == sizeof(small_type));
};
template<class T>
struct has_foo_static_member_function
{
template<int (*)()> struct tester;
template<class U> static small_type has_foo(tester<&U::foo> *);
template<class U> static large_type has_foo(...);
static const bool value = (sizeof(has_foo<T>(0)) == sizeof(small_type));
};
template<class T>
struct has_foo_member_variable
{
template<int T::*> struct tester;
template<class U> static small_type has_foo(tester<&U::foo> *);
template<class U> static large_type has_foo(...);
static const bool value = (sizeof(has_foo<T>(0)) == sizeof(small_type));
};
template<class T>
struct has_foo_static_member_variable
{
template<int *> struct tester;
template<class U> static small_type has_foo(tester<&U::foo> *);
template<class U> static large_type has_foo(...);
static const bool value = (sizeof(has_foo<T>(0)) == sizeof(small_type));
};
template<class T>
void print()
{
std::cout << has_foo_member_function<T>::value << " "
<< has_foo_static_member_function<T>::value << " "
<< has_foo_member_variable<T>::value << " "
<< has_foo_static_member_variable<T>::value << "\n";
}
struct A
{
int foo()
{
return 0;
}
};
struct B
{
static int foo()
{
return 0;
}
};
struct C
{
int foo;
};
struct D
{
static int foo;
};
int main()
{
print<A>();
print<B>();
print<C>();
print<D>();
}