随着 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
给出这个(同样,T
到 int *
工作正常):
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/