Too few problems with template options

Can anyone tell me how to make the following pseudocode compatible with GCC4 ? I wonder how it works in MSVC ...

typedef int TypeA;
typedef float TypeB;

class MyClass
{
// No base template function, only partially specialized functions...
    inline TypeA myFunction<TypeA>(int a, int b) {} //error: Too few template-parameter-lists
    template<> inline TypeB myFunction<TypeB>(int a, int b) {}
};
+3
source share
1 answer

The correct way to code this construct is:

typedef int TypeA;
typedef float TypeB;
class MyClass
{
    template <typename T> 
    T myFunction( int a, int b );
};
template <> 
inline TypeA MyClass::myFunction<TypeA>(int a, int b) {}
template <> 
inline TypeB MyClass::myFunction<TypeB>(int a, int b) {}

Note that the template member must be declared inside the class declaration, but specializations must be defined outside of it at the namespace level.

+5
source

All Articles