【发布时间】:2014-02-15 19:29:41
【问题描述】:
我去看看你是否可以在变量模板声明中使用 auto。
template <typename T>
auto F = T{};
很好,但是一旦你尝试使用它,就会发出嘀嘀咕咕的声音。
int f = F<int>; // error: cannot initialize a variable of type 'int' with an lvalue of type 'auto'
auto f = F<int>; // Stacktrace
decltype(F<int>) f = F<int>; // StackFace
std::cout << std::is_same<int, decltype(F<int>)>::value; // false
std::cout << typeid(decltype(F<int>)).name(); // Stacktrace
std::cout << std::is_same<decltype(F<int>), decltype(F<int>)>::value; // true
decltype(auto)、auto 的任何组合都不起作用,即使它说auto 是一个左值。
int f = static_cast<int>(F<int>); // error: static_cast from 'auto' to 'int' is not allowed
我以前从未见过自动这样做。是因为模板变量的原因,还是因为 clang 对待 auto 的方式?
【问题讨论】:
-
我认为它可能还没有完全实现。
-
"// Stacktrace" 每当你得到那个消息时,消息“clang: note: diagnostic msg: PLEASE submit a bug report to llvm.org/bugs and include crash backtrace, preprocessed source, and associated run script 。”包含在输出中。请这样做,这显然是 clang 中的一个错误,并且 clang 开发人员比 SO 上的人们更有可能给出有用的响应。
标签: c++ clang c++14 variable-templates