As quoted in dcl.struct.bind,
Let cv denote the cv-qualifiers in the decl-specifier-seq.
Designating the non-static data members of E as m 0 , m 1 , m 2 , ... (in declaration order), each v i is the name of an lvalue that refers to the member m i of e and whose type is cv T i , where T i is the declared type of that member;
If I'm understanding correctly, the cv-qualifiers are propagated from the declartion of structured binding.
Say I have a simple struct,
struct Foo {
int x;
double y;
};
Consider the two scenarios,
const Foo f{1, 1.0};
auto& [x, y] = f;
// static_assert(std::is_same<decltype(x), int>::value); // Fails!
static_assert(std::is_same<decltype(x), const int>::value); // Succeeds
Live Demo.
Does the cv-qualifier of x
come from the deduction auto
?
The second one,
Foo f{1, 1.0};
const auto& [x, y] = f;
const auto& rf = f;
static_assert(std::is_same<decltype(x), const int>::value); // with const
static_assert(std::is_same<decltype(rf.x), int>::value); // without const
Live Demo. The result complies with the standard, which makes sense.
My second question is is there any reason to propagate the cv-qualifiers, isn't it a kind of inconsistent (to the initialization a reference with auto
)?
Foo f
is const, in the code posted here - it's not. Please decide ;) – Handsetdecltype
has special rules for unparenthesis id-expression – Orcein