如何在 class 声明之外定义 sfinae class 的成员?

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

在阅读了诸如 sfinae on member function defined outside of class body(这不是同一个问题)和其他问题之后,我仍然没有找到在 [=26 之外定义成员函数主体的好方法=] 使用 SFINAE 方法启用仅具有算术类型的 class 时的声明。

#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 ()
{
}

在这个例子中,我得到了错误:

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

而如果我这样声明:

#include <type_traits>

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

它运行起来没有任何问题。

我正在使用 mingw-w64 (w64 3.3) 来编译这段代码。

foo 有两个模板参数,即使其中一个未命名、默认并用于 SFINAE。因此:

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