6

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.

Tom Huntington
  • 2,260
  • 10
  • 20
  • 3
    strange, because [insigts shows it as a reference](https://cppinsights.io/lnk?code=I2luY2x1ZGUgPGFycmF5PgojaW5jbHVkZSA8dHlwZV90cmFpdHM+CgoKaW50IG1haW4oKQp7CiAgICBhdXRvIGEgPSAwOwogICAgYXV0byYgYV9yZWYgPSBhOwogICAgc3RhdGljX2Fzc2VydChzdGQ6OmlzX3JlZmVyZW5jZV92PGRlY2x0eXBlKGFfcmVmKT4pOwoKICAgIGF1dG8gcCA9IHN0ZDo6cGFpcnsgMSwgMi5mIH07CglhdXRvJiYgW2ksIGRdID0gcDsKCS8vIE5PVEUgVEhFIE5PVAoJc3RhdGljX2Fzc2VydChub3Qgc3RkOjppc19yZWZlcmVuY2VfdjxkZWNsdHlwZShpKT4pOwoJCn0=&insightsOptions=cpp17&std=cpp17&rev=1.0). This is a good question! – Fantastic Mr Fox Sep 28 '22 at 23:14

1 Answers1

4

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.

Fantastic Mr Fox
  • 32,495
  • 27
  • 95
  • 175