0

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 :)

Community
  • 1
  • 1
Wojciech Danilo
  • 11,573
  • 17
  • 66
  • 132
  • 3
    You link to my answer, but my answer does not contain that `typeof` thing. – Johannes Schaub - litb Nov 16 '10 at 23:46
  • 1
    One, I'm surprised this gives you an error about `test` being a non-const member - both instances of test look like function declarations to me. Two, this isn't reflection. Reflection implies runtime discovery of types, methods, members, etc; this is all done statically at compile-time. – Nathan Ernst Nov 17 '10 at 01:27

1 Answers1

2

Your trouble is with the use of the integral zero and typeof. Using MSVC10, decltype, and nullptr, it's a trivial modification for this code to print the correct values.

template <typename T>
class has_helloworld
{
    typedef char one;
    typedef long two;

    template <typename C> static one test( decltype(&C::helloworld) ) ;
    template <typename C> static two test(...);


public:
    enum { value = std::is_same<decltype(test<T>( nullptr )), char>::value };
};


int main(int argc, char *argv[])
{
    std::cout << has_helloworld<Hello>::value << std::endl;
    std::cout << has_helloworld<Generic>::value << std::endl;
    std::cin.get();
    return 0;
}

I'm not an expert on typeof, but something like this should be doable:

struct no_type {};
// SFINAE test
template <typename T>
class has_helloworld
{
    template <typename C> static typeof(&C::helloworld) test( C* i );
    template <typename C> static no_type test(...);


public:
    enum { value = std::is_same<no_type, typeof(test<T>(0))>::value };
};
Puppy
  • 144,682
  • 38
  • 256
  • 465