1

I have some old codebase that still uses C++03. I am trying to write a template that evaluates to true if a class has a member function called some_function, to be used inside enable_if statements. I cannot make my example work, i.e. the output is always zero. Can anyone spot the error and explain why this fails, and how to fix it?

#include <iostream>

template< typename T >
struct has_some_function
{
    typedef char yes[1];
    typedef char no[2];
    template< typename C > static yes& test(typename C::some_function*);
    template< typename C > static no& test(...);
    static const bool value = (sizeof(test< T >(0)) == sizeof(yes));
};

struct Test
{
    void some_function()
    {
    }
};

int main()
{
    std::cout << has_some_function< Test >::value << std::endl;
}

With

g++ -std=c++03 test.cpp

I get

0
hochl
  • 12,524
  • 10
  • 53
  • 87

0 Answers0