我正在使用GCC 5.4 ( C++14 )编译以下C++14代码:
template<typename T>
struct traits {
template<typename X>
static constexpr bool vect = true;
};
template<typename T1, typename T2>
constexpr bool all_vect = traits<T1>::template vect<T2>;
bool something() {
return all_vect<void, double>;
}但我犯了以下错误:
<source>: In instantiation of 'constexpr const bool all_vect<void, double>':
<source>:11:12: required from here
<source>:8:16: error: 'template<class X> constexpr const bool traits<void>::vect<X>' is not a function template
constexpr bool all_vect = traits<T1>::template vect<T2>;
^
<source>:8:16: error: 'vect<T2>' is not a member of 'traits<void>'
Compiler exited with result code 1而我在GCC 6.1或更多,或更佳3.9以上没有问题。但是对于我尝试过的所有版本的GCC5来说都是一样的。
我找不到原因吗?通常,GCC5应该是完整的C++14特性。
对于仍然使用变量模板的GCC5中的这个问题,有简单的解决办法吗?我不想再使用简单的特性,因为我正在将我的所有特性转换为使用可变模板。
发布于 2017-08-10 07:53:44
它是在gcc6中修复的一个bug,如dupe中所示。
在保留模板变量的同时,似乎没有解决办法。
对于避免使用变量模板的解决方案,可以使用好的旧静态、非模板化变量:
template<typename T>
struct traits {
template<typename X>
struct Is_vect
{
static constexpr bool value = true;
};
};
template<typename T1, typename T2>
struct Are_all_vect
{
static constexpr bool value = traits<T1>::template Is_vect<T2>::value;
};
bool something() {
return Are_all_vect<void, double>::value;
}或警察的模板功能:
template<typename T>
struct traits {
template<typename X>
static constexpr bool vect() { return true; }
};
template<typename T1, typename T2>
constexpr bool all_vect() { return traits<T1>::template vect<T2>(); }
bool something() {
return all_vect<void, double>();
}https://stackoverflow.com/questions/45607450
复制相似问题