我需要在模板类中初始化一个静态bool,我试着像this那样做。我能看到的唯一区别是我对类型参数T有一个约束,但这会导致编译错误,为什么?我该如何解决这个问题?
代码如下:
template<class T, class = typename enable_if<is_any_integral<T>::value>::type>
class fraction {
static bool auto_reduce;
// ...
};
template<class T, class = typename enable_if<is_any_integral<T>::value>::type>
bool fraction<T>::auto_reduce = true;
错误是:
错误:嵌套名称说明符&#39;
fraction<T>::
&#39;声明不引用类,类模板或类模板部分特化
bool fraction<T>::auto_reduce = true;
答案 0 :(得分:2)
Maybe simpler
template<class T, class V>
bool fraction<T, V>::auto_reduce = true;
When you write
template<class T, class = typename enable_if<is_any_integral<T>::value>::type>
class fraction
you say that fraction
is a class with two type template paramenters; the std::enable_if
if part is useful to assign a default value to the second parameter (and to permit the enable/not enable SFINAE works) but fraction
is and remain a template class
with two parameters, you have to cite both and there is no need to repeat the enable/not enable/default part for second parameter initializing auto_reduce
.