Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

decltype does not preserve ref qualifier from structured binding

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

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

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

>Solution :

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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading