c++ - GCC5 : Nested variable template is not a function template?

标签 c++ gcc c++14

<分区>

我正在尝试使用 GCC 5.4 ( online example ) 编译以下 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 或更高版本或 clang 3.9 或更高版本中没有问题。但是我试过的所有版本的 GCC5 都是一样的。

我没有找到原因?通常,GCC5 应该是 C++14 功能完整的。

对于 GCC5 中仍然使用变量模板的这个问题,是否有简单的解决方法?我不想回到使用简单特征,因为我正在将我所有的特征转换为使用变量模板。

最佳答案

如 dupe 所示,这是 gcc6 中修复的错误。

看起来在保留模板变量的同时没有解决方法。

对于避开变量模板的解决方法,您可以使用良好的旧静态非模板变量:

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;
}

或 constexpr 模板函数:

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>();
}

关于c++ - GCC5 : Nested variable template is not a function template?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45607450/

相关文章:

CC\GCC 编译后的可执行文件无法在我的机器上运行?

gcc - 使用 libc 库进行编译

c++ - inet_aton() 对无效 IP 地址返回成功?

c++ - 映射或设置具有透明比较器和异构意义上的非唯一元素

c++ - 返回 const std::string 真的比非 const 慢吗?

c++ - 我将如何使用初始值设定项列表直接创建我的链接列表?

c++ - 通过函数指针调用 C++ 成员函数

c++ - 将 std::error_code 与整数进行比较

c++ - 如何从 C++ 中的多组结构中删除元素?

c++ - 将现代函数接口(interface)写入 "produce a populated container"