Detect if class has overloaded function fails on Comeau compiler

Posted by Frank on Stack Overflow See other posts from Stack Overflow or by Frank
Published on 2010-05-04T19:50:53Z Indexed on 2010/05/04 20:18 UTC
Read the original article Hit count: 157

Filed under:
|
|
|

Hi Everyone,

I'm trying to use SFINAE to detect if a class has an overloaded member function that takes a certain type. The code I have seems to work correctly in Visual Studio and GCC, but does not compile using the Comeau online compiler.

Here is the code I'm using:

#include <stdio.h>

//Comeau doesnt' have boost, so define our own enable_if_c
template<bool value> struct enable_if_c { typedef void type; };
template<> struct enable_if_c< false > {}; 


//Class that has the overloaded member function
class TestClass
{
public:
    void Func(float value) { printf( "%f\n", value ); }
    void Func(int value) { printf( "%i\n", value ); }
};


//Struct to detect if TestClass has an overloaded member function for type T
template<typename T>
struct HasFunc
{
    template<typename U, void (TestClass::*)( U )> struct SFINAE {};
    template<typename U> static char Test(SFINAE<U, &TestClass::Func>*);
    template<typename U> static int Test(...);
    static const bool Has = sizeof(Test<T>(0)) == sizeof(char);
};


//Use enable_if_c to only allow the function call if TestClass has a valid overload for T
template<typename T> typename enable_if_c<HasFunc<T>::Has>::type CallFunc(TestClass &test, T value) { test.Func( value ); } 

int main()
{
    float value1 = 0.0f;
    int value2 = 0;
    TestClass testClass;
    CallFunc( testClass, value1 );  //Should call TestClass::Func( float )
    CallFunc( testClass, value2 );  //Should call TestClass::Func( int )
}

The error message is: no instance of function template "CallFunc" matches the argument list. It seems that HasFunc::Has is false for int and float when it should be true.

Is this a bug in the Comeau compiler? Am I doing something that's not standard? And if so, what do I need to do to fix it?

© Stack Overflow or respective owner

Related posts about c++

Related posts about templates