考虑这段代码(demo https://godbolt.org/z/Nmz-gN):
#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?)
}
A 结构化绑定到一个副本const
声明了 c 数组const由 Clang 和非常量 by GCC.
GCC 对 c 数组的行为与对聚合或类元组类型观察到的行为一致。
另一方面,从我对标准的阅读来看,我认为 Clang 遵循了所写的内容。在[dcl.struct.bind]/1 http://eel.is/c++draft/dcl.struct.bind#1.sentence-4 e 具有 cv A 型其中 A 是初始化表达式的类型,cv是结构化绑定声明的 cv 限定符。以及初始化表达式的类型ar
相应地[表达式类型]/1 http://eel.is/c++draft/expr.type#1.sentence-1 const int[2]
.
应该期待什么?我的观点是 Clang 遵循标准。另一方面,我觉得其意图是数组、聚合和类元组类型的行为是等效的。