Pie*_*ume 2 templates sfinae c++11
在阅读了诸如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 ()
{
}
Run Code Online (Sandbox Code Playgroud)
在此示例中,我收到错误:
error: invalid use of incomplete type 'class foo<T>'
void foo<T>::bar ()
^
error: declaration of 'class foo<T>'
class foo
^
Run Code Online (Sandbox Code Playgroud)
而如果我这样声明:
#include <type_traits>
template <typename T,typename = typename std::enable_if<std::is_arithmetic<T>::value,T>::type>
class foo
{
public:
void bar()
{
}
};
Run Code Online (Sandbox Code Playgroud)
它运行起来没有任何问题。
我正在使用 mingw-w64 (w64 3.3) 来编译此代码。
foo有两个模板参数,即使其中之一未命名,也默认用于 SFINAE。因此:
template <typename T, typename U>
void foo<T, U>::bar ()
{
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
660 次 |
| 最近记录: |