c++ - 我可以使用一个变量模板来声明另一个变量模板吗?

标签 c++ c++14 variable-templates

随着 C++14 中的变量模板(Clang 已经支持它们)和标准提案 is_same_v同样是类型特征,我认为能够按如下方式制作新的类型特征会很好:

template<typename T>
constexpr bool is_const_and_volatile{std::is_const_v<T> && std::is_volatile_v<T>};

唉,这会导致错误等同于以下 SSCCE(this one 包含下面提到的所有内容):

#include <type_traits>

template<typename T>
constexpr bool is_pointer{std::is_pointer<T>::value};

template<typename T>
constexpr bool foo{is_pointer<T>};

int main() {
    //foo<int *>;
}

使用 main 中的行评论时,Clang 吐出以下内容:

warning: variable is_pointer<type-parameter-0-0> has internal linkage but is not defined

它在我看来很明确(请注意,在 T 中将 int * 更改为 foo 效果很好)。取消注释 main 中的行实例化 foo给出这个(同样,Tint * 工作正常):

error: constexpr variable foo<int *> must be initialized by a constant expression

但是,替换 foo使用以下旧语法会导致两个实例都正常工作:

constexpr bool foo{std::is_pointer<T>::value};

关于变量模板,我是否遗漏了什么?有没有办法它们来构建新的变量模板,或者我是否被迫使用旧语法来构建新的模板,并且只有在将它们用于其他代码时才享受语法糖?

最佳答案

您的代码有效,并被 clang SVN 接受。链接错误是由 clang bug 17846 引起的,我fixed a couple of days ago .

关于c++ - 我可以使用一个变量模板来声明另一个变量模板吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21358810/

相关文章:

c++ - double /整数使用和基于范围的 for 循环中的数据缩小

c++ - 变量模板的开销

c++ - 使用 SFINAE 的 Variadic 变量模板特化

c++ - 溅射结构

c++函数语法/原型(prototype) - 括号后的数据类型

c++ - find_if 无法检测到 vector 为空

c++ - (c++14) operator<< 重载不能按智能指针 vector 的预期工作

c++ - 如何创建一个包含4个字节的多个信息的ID

c++ - cout奇怪的输出

c++ - C++ 引用是否保证使用指针 "internally"?