【发布时间】:2016-06-30 03:36:28
【问题描述】:
我正在尝试编译以下代码:
template <class T, typename std::enable_if<!std::is_fundamental<T>::value, int >::type = 0 >
class Calc
{
public:
int operator()( const T& v ) const {
return v.getValue();
}
};
template <class T, typename std::enable_if<std::is_fundamental<T>::value, int >::type = 0 >
class Calc : CalcBase <T>
{
};
编译时出现以下错误:
c.cpp:26: error: template parameter 'typename std::enable_if<(! std::is_fundamental::value), int>::type <anonymous>'
c.cpp:36: error: redeclared here as 'typename std::enable_if<std::is_fundamental::value, int>::type <anonymous>'
如果传递的模板参数是一个类,这里的目的是选择覆盖基类函数调用运算符的 Calc 版本。如果传递的参数是基本类型,那么我们选择不覆盖基类功能的 Calc 版本。 你能帮我理解我如何才能让它工作吗?
【问题讨论】:
-
您需要展示更多代码。目前尚不清楚什么应该是主要模板,什么是/是专业化
-
还不清楚这应该是什么意思:
typename std::enable_if<std::is_fundamental<T>::value, int >::type = 0类型何时以及为什么会等于零?通常我会认为答案是,永远不会。 -
@ChrisBeck 在他的例子中,
type是int的别名,int可以是一个非类型模板参数,它可以有一个默认值(例如 0)。这实际上是从参与重载解析中删除函数的一种方法,因为当std::enable_if<>的条件为假时,没有type别名并且SFINAE 发生。 -
我明白了,我以前从未见过那个。比较常见的是
typename ENABLE = typename std::enable_if<...>::type -
@ChrisBeck 和@user2296177。感谢您花时间查看我的问题。 Dietmar Kühl 提供了一个解决方案,解决了我的问题。感谢您的帮助。
标签: c++ templates c++11 stl enable-if