【发布时间】:2018-12-11 10:06:20
【问题描述】:
struct X { int a, b; };
int main()
{
auto p = std::pair{ 1, 2 };
const auto&[r1, r2] = p; // ok
X x{ 1, 2 };
const auto&[r3, r4] = x; // error
}
clang 7.0 (on Windows) 的错误信息:
error : cannot decompose this type; 'std::tuple_size<const X>::value' is not a valid
integral constant expression
为什么结构化绑定在 struct 上不能按预期工作?
【问题讨论】:
-
Cannot reproduce,甚至没有clang。
-
Works on Clang 5.0。这是一个错误。
-
linux 上的 clang 6 出现同样的错误。它无法编译。
-
(问题是,核心语言指定结构化绑定声明在定义
std::tuple_size<T>时使用std::tuple_size<T>::value,标准库为所有const类型T定义std::tuple_size<T>。)
标签: c++ alias standards c++17 structured-bindings