Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>From compiler point of view references are nothing else but const pointers. Rewrite your example with pointers and it becomes clear how and why it works:</p> <pre><code>struct A; struct B { A* a; }; struct A { B* b; }; struct {A first; B second;} pair = {&amp;(pair.second), &amp;(pair.first)}; //parentheses for clarity </code></pre> <p>As Schollii wrote: memory is allocated beforehand, thus addressable. There is no access nor evaluation because of references/pointers. That's merely taking addresses of "second" and "first", simple pointer arithmetics.</p> <p><em>I could rant about how using references in any place other than operator is language abuse, but I think this example highlights the issue well enough :)</em></p> <p>(From now on I write all the ctors manually. Your compiler may or may not do this automagically for you.) Try using new:</p> <pre><code>struct A; struct B { A&amp; a; B(A&amp; arg):a(arg){;} }; struct A { B&amp; b; A(B&amp; arg):b(arg){;} }; typedef struct PAIR{A first; B second; PAIR(B&amp; argB, A&amp; argA):first(argB),second(argA){;}} *PPAIR, *const CPPAIR; PPAIR pPair = NULL;// just to clean garbage or 0xCDCD pPair = new PAIR(pPair-&gt;second, pPair-&gt;first); </code></pre> <p>Now it depends on order of execution. If assignment is made last (after ctor) the second.p will point to 0x0000 and first.ref to e.g. 0x0004.<br> Actually, <a href="http://codepad.org/yp911ug6" rel="nofollow">http://codepad.org/yp911ug6</a> here it's the ctors which are run last (makes most sense!), therefore everything works (even though it appears it shouldn't).</p> <p>Can't speak about templates, though.</p> <p>But your question was "Is that legal?". No law forbids it.<br> Will it work? Well, I don't trust compiler makers enough to make any statements about that.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload