I've found some very interesting c++ code on stackoverflow and I'm very confused about it because as author says it should work and it fails on my gcc 4.5.1 and 4.4 :( The goal is to check if class contain or not a specific method.
the code is:
#include <iostream>
struct Hello
{
int helloworld()
{ return 0; }
};
struct Generic {};
// SFINAE test
template <typename T>
class has_helloworld
{
typedef char one;
typedef long two;
template <typename C> static one test( typeof(&C::helloworld) ) ;
template <typename C> static two test(...);
public:
enum { value = sizeof(test<T>(0)) == sizeof(char) };
};
int
main(int argc, char *argv[])
{
std::cout << has_helloworld<Hello>::value << std::endl;
std::cout << has_helloworld<Generic>::value << std::endl;
return 0;
}
I've got compiler error:
ISO C++ forbids in-class initialization of non-const static member 'test'
Additional as some comment says - I can change 'typeof(&C::helloworld)' to 'char[sizeof(&C::helloworld)]' but then my output is
1
1
what is wrong, because only one class has helloworld function
Is there any method to make it working? Addition I would be very thankfull if somebody could explain what exactly makes this command:
test( char[sizeof(&C::helloworld)] ) ;
Thank you very much :)