Pierre-Antoine Guillaume
Pierre-Antoine Guillaume

Reputation: 1136

How to define member of sfinae class outside of class declaration?

After reading questions such as sfinae on member function defined outside of class body (which isn't the same question), and other, I still don't get the good way to define the body of a member function outside the class declaration when using a SFINAE approach to enable the class only with arithmetic types.

#include <type_traits>

template <typename T,typename = typename std::enable_if<std::is_arithmetic<T>::value,T>::type>
class foo
{
public:
    void bar();
};

template <typename T>
void foo<T>::bar ()
{
}

In this example, I get the error :

error: invalid use of incomplete type 'class foo<T>'
void foo<T>::bar ()
^
error: declaration of 'class foo<T>'
class foo
^

Whereas if I declare it like so :

#include <type_traits>

template <typename T,typename = typename std::enable_if<std::is_arithmetic<T>::value,T>::type>
class foo
{
public:
    void bar()
    {
    }
};

It functions without any trouble whatsoever.

I am using mingw-w64 (w64 3.3) to compile this code.

Upvotes: 2

Views: 700

Answers (1)

Quentin
Quentin

Reputation: 63154

foo has two template parameters, even if one of them is unnamed, defaulted and used for SFINAE. Hence:

template <typename T, typename U>
void foo<T, U>::bar ()
{
}

Upvotes: 5

Related Questions