开发者

Problem while checking if function exist in c++

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 开发者_C百科'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 :)


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 };
};
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜