Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The problem here is a bug / misfeature / hole in the C++03 standard, with different compilers trying to patch over the problem in different ways. (This problem no longer exists in C++11 standard.)</p> <p>Sections 8.5.3/5 of both standards specify how a reference is initialized. Here's the C++03 version (the list numbering is mine):</p> <blockquote> <p>A reference to type <code>cv1 T1</code> is initialized by an expression of type <code>cv2 T2</code> as follows:</p> <ol> <li><p>If the initializer expression</p> <ol> <li>is an lvalue (but is not a bit-field), and “cv1 T1” is reference-compatible with “cv2 T2,” or</li> <li>has a class type (i.e., <code>T2</code> is a class type) and can be implicitly converted to an lvalue of type <code>cv3 T3</code>, where <code>cv1 T1</code> is reference-compatible with <code>cv3 T3</code></li> </ol> <p>then the reference is bound directly to the initializer expression lvalue in the first case, and the reference is bound to the lvalue result of the conversion in the second case.</p></li> <li><p>Otherwise, the reference shall be to a non-volatile const type (i.e., <code>cv1</code> shall be <code>const</code>).</p></li> <li><p>If the initializer expression is an rvalue, with <code>T2</code> a class type, and <code>cv1 T1</code> is reference-compatible with <code>cv2 T2</code>, the reference is bound in one of the following ways (the choice is implementation-defined):</p> <ol> <li>The reference is bound to the object represented by the rvalue (see 3.10) or to a sub-object within that object.</li> <li>A temporary of type <code>cv1 T2</code> [sic] is created, and a constructor is called to copy the entire rvalue object into the temporary. The reference is bound to the temporary or to a sub-object within the temporary.</li> </ol> <p>The constructor that would be used to make the copy shall be callable whether or not the copy is actually done.</p></li> <li><p>Otherwise, a temporary of type <code>cv1 T1</code> is created and initialized from the initializer expression using the rules for a non-reference copy initialization (8.5). The reference is then bound to the temporary.</p></li> </ol> </blockquote> <p>There are three types involved in the question at hand:</p> <ul> <li>The type of the reference to be created. The standards (both versions) denote this type as <code>T1</code>. In this case, it is <code>struct A</code>.</li> <li>The type of the initializer expression. The standards denote this type as <code>T2</code>. In this case, the initializer expression is the variable <code>c</code>, so <code>T2</code> is <code>struct C</code>. Note that because <code>struct A</code> is not <em>reference-compatible</em> with <code>struct C</code>, it's not possible to directly bind the reference to <code>c</code>. An intermediate is needed.</li> <li>The type of the intermediate. The standards denote this type as <code>T3</code>. In this case, this is <code>struct B</code>. Note that applying the conversion operator <code>C::operator B()</code> to <code>c</code> will convert the lvalue <code>c</code> to an rvalue.</li> </ul> <p>The initializations by what I labeled as 1.1 and 3 are out because the <code>struct A</code> is not reference-compatible with <code>struct C</code>. The conversion operator <code>C::operator B()</code> needs to be used. 1.2 is out Because this conversion operator returns an rvalue, this rules 1.2 out. All that is left is option 4, create a temporary of type <code>cv1 T1</code>. Strict compliance with the 2003 version of the standard forces the creation of two temporaries for this problem, even though only one will suffice.</p> <p>The 2011 version of the standard fixes the problem by replacing option 3 with</p> <blockquote> <ul> <li><p>If the initializer expression</p> <ul> <li>is an xvalue, class prvalue, array prvalue or function lvalue and <code>cv1 T1</code> is reference- compatible with <code>cv2 T2</code>, or</li> <li>has a class type (i.e., <code>T2</code> is a class type), where <code>T1</code> is not reference-related to <code>T2</code>, and can be implicitly converted to an xvalue, class prvalue, or function lvalue of type <code>cv3 T3</code>, where <code>cv1 T1</code> is reference-compatible with <code>cv3 T3</code>,</li> </ul> <p>then the reference is bound to the value of the initializer expression in the first case and to the result of the conversion in the second case (or, in either case, to an appropriate base class subobject). In the second case, if the reference is an rvalue reference and the second standard con- version sequence of the user-defined conversion sequence includes an lvalue-to-rvalue conversion, the program is ill-formed.</p></li> </ul> </blockquote> <p>It appears that the gcc family of compilers chose strict compliance over intent (avoid creating unnecessary temporaries), while the other compilers that print "b" chose intent / corrections to the standard. Choosing strict compliance isn't necessarily commendable; there are other bugs/misfeatures in the 2003 version of the standard (e.g., <code>std::set</code>) where the gcc family chose sanity over strict compliance.</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. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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