【发布时间】:2017-06-02 05:00:45
【问题描述】:
在阅读了诸如sfinae on member function defined outside of class body(这不是同一个问题)之类的问题后,当使用 SFINAE 方法时,我仍然没有得到在类声明之外定义成员函数体的好方法仅使用算术类型启用该类。
#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) 来编译这段代码。
【问题讨论】: