8

In [dcl.struct.bind] 9.6.4, there is definition of structured binding when initializer is a class type with std​::​tuple_­size<E>​::​value properly defined:

... variables are introduced with unique names ri as follows:
S Ui ri = initializer ;
Each vi is the name of an lvalue of type Ti that refers to the object bound to ri; the referenced type is Ti.

My question is why is it necessary to introduce ri, can't we define the identifier vi directly as reference to the result of get<i>(e)?

Masquue
  • 166
  • 8
  • I would guess the reason is because otherwise you'd get dangling references, as the lifetime of (the resulting object of) the initializer ends with the declaration. – Passer By Mar 19 '22 at 06:01
  • @PasserBy I guess the initializer (not the initializer in the above quote, but in the whole structured binding declaration) has already been extended or copied by the introduced `e` variable? Then the subobjects will never be dangling? – Masquue Mar 19 '22 at 06:05
  • Oh no, my quote is from C++20. And in [C++17](https://timsong-cpp.github.io/cppwp/n4659/dcl.struct.bind#:~:text=Given%20the%20type,.), _vi_ really is defined as a reference. – Masquue Mar 19 '22 at 13:23

1 Answers1

5

The intent is to disallow redeclaring structured bindings as references. See CWG 2313.

cpplearner
  • 13,776
  • 2
  • 47
  • 72
  • Thank you very much. May I ask where can I find the information like git blame about which part of the standard is changed due to which proposal for what reason? – Masquue Mar 19 '22 at 13:07
  • 2
    @Masquue The source of the standard is available at https://github.com/cplusplus/draft. You can literally use `git blame` with that. – cpplearner Mar 19 '22 at 13:45
  • @Masquue Isn't the question about why introduce `ri` at all and not just say that `vi` is an lvalue with the same type and result as `get(e)`? – Language Lawyer Mar 19 '22 at 14:41
  • @LanguageLawyer Yes, it's about why introduce `ri`. I don't get what's your question... – Masquue Mar 19 '22 at 14:45
  • @Masquue _Yes, it's about why introduce `ri`_ Then I don't get how this answer answers your question. – Language Lawyer Mar 19 '22 at 14:57
  • @LanguageLawyer Previous wording allows redeclaring the structured binding which is disallowed by current wording. I think this answer and the link says it clearly... – Masquue Mar 19 '22 at 15:05