阅读完诸如此类的问题后sfinae 位于类体外部定义的成员函数(这不是同一个问题),另外,当使用 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) 来编译此代码。
foo
有两个模板参数,即使其中之一未命名,也默认用于 SFINAE。因此:
template <typename T, typename U>
void foo<T, U>::bar ()
{
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)