Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

decltype does not preserve ref qualifier from structured binding [duplicate]

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.

like image 899
Tom Huntington Avatar asked Aug 07 '26 06:08

Tom Huntington


1 Answers

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.

like image 50
Fantastic Mr Fox Avatar answered Aug 10 '26 11:08

Fantastic Mr Fox



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!