【发布时间】:2019-05-12 13:28:16
【问题描述】:
考虑一下这段代码 (demo):
#include <tuple>
#include <type_traits>
struct Ag{int i;int j;};
using T = std::tuple<int,int>;
using Ar = int[2];
const Ag ag {};
const T t {};
const Ar ar {};
void bind_ag(){
auto [i,j] = ag;
static_assert(std::is_same_v<decltype((i)),int&>);
}
void bind_t(){
auto [i,j] = t;
static_assert(std::is_same_v<decltype((i)),int&>);
}
void bind_ar(){
auto [i,j] = ar;
static_assert(std::is_same_v<decltype((i)),int&>); //For GCC
static_assert(std::is_same_v<decltype((i)),const int&>); //For Clang (and standard?)
}
结构化绑定到const c-array 的副本由 Clang 声明为 const,由 GCC 声明为 non-const。
GCC 对 c 数组的行为与观察到的聚合或类元组类型的行为一致。
另一方面,根据我对标准的阅读,我认为 Clang 遵循所写的内容。在[dcl.struct.bind]/1 中,e 的类型为 cv A,其中 A 是初始化表达式的类型,cv 是结构化绑定声明的 cv 限定符。而初始化表达式ar的类型对应于[expr.type]/1const int[2]。
应该期待什么?我的观点是 Clang 遵循标准。另一方面,我觉得其意图是数组、聚合和类似元组的类型的行为是等效的。
【问题讨论】:
标签: c++ language-lawyer c++17 structured-bindings