The*_* do 12 c++ templates metaprogramming sfinae
在代码中:
template<class T>
struct is_builtin
{
enum {value = 0};
};
template<>
struct is_builtin<char>
{
enum {value = 1};
};
template<>
struct is_builtin<int>
{
enum {value = 1};
};
template<>
struct is_builtin<double>
{
enum {value = 1};
};
template<class T>
struct My
{
typename enable_if<is_builtin<T>::value,void>::type f(T arg)
{
std::cout << "Built-in as a param.\n";
}
typename enable_if<!is_builtin<T>::value,void>::type f(T arg)
{
std::cout << "Non - built-in as a param.\n";
}
};
struct A
{
};
int main()
{
A a;
My<int> m;
My<A> ma;
m.f(1);
ma.f(a);
return 0;
}
Run Code Online (Sandbox Code Playgroud)
我收到一个错误:
error C2039: 'type' : is not a member of 'std::tr1::enable_if<_Test,_Type>'
Run Code Online (Sandbox Code Playgroud)
显然我不懂如何使用enable_if
.我在想的是我可以在编译期间从一组成员函数中启用一个或第二个成员函数,但它不起作用.任何人都可以向我解释如何正确地做到这一点?
编辑
我真正无法理解的是为什么不存在typedef
其中一个def.编译器找不到它,它不会编译它.
Jam*_*lis 13
您不能使用类模板参数来获取成员函数的SFINAE.
你要么需要
使成员函数成为成员函数模板,并使用enable_if
成员函数模板的模板参数或
将成员函数移动f
到策略类中并使用特殊化类模板enable_if
.
归档时间: |
|
查看次数: |
6411 次 |
最近记录: |