Home > Software engineering >  decltype does not preserve ref qualifier from structured binding
decltype does not preserve ref qualifier from structured binding

Time:09-29

Usually decltype perseveres the ref qualifiers

auto a = 0;
auto& a_ref = a;
static_assert(std::is_reference_v<decltype(a_ref)>);

But apparently not when it's argument is obtained from structured binding

auto p = std::pair{1, 2.f};
auto& [i, d] = p;
static_assert(std::is_reference_v<decltype(i)>); // fails

https://godbolt.org/z/qWT574fr9

I'm pretty sure that i and d are references here. They should be according to oldnewthing and intellisense is telling me so.

CodePudding user response:

Looks like this is how it is supposed to be:

decltype(x), where x denotes a structured binding, names the referenced type of that structured binding. In the tuple-like case, this is the type returned by std::tuple_element, which may not be a reference even though a hidden reference is always introduced in this case.

From cpprefence

In your case, you are using a pair, but i am pretty sure this falls under tuple like type.

  • Related