Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I would assume that your version of GCC trips over the fact that <code>TestClass().str</code> is an <em>rvalue</em> array (quite an exotic thing in both C and C++ languages). Something in the specifics of reference initialization when the initailizer is an <em>rvalue</em> is implemented incorrectly. Apparently, the bug was fixed in later versions.</p> <p>Meanwhile both <code>obj.str</code> and <code>TestClass().getStr()</code> are <em>lvalue</em> arrays. They are handled by the straightforward rules of direct reference binding, so the issue does not arise.</p> <p><em>To address the comment:</em> </p> <p>If you modify your function template to </p> <pre><code>template &lt;typename T&gt; printFunc(const T str) </code></pre> <p>the situation will change drastically. Now it is no longer a reference-binding issue. Now the array-to-pointer conversion rules take over. </p> <p>In your original version the type <code>T</code> was deduced as <code>char [6]</code>, while in this new one it is deduced as <code>char *</code>. Your array type is completely lost when the argument is passed to the function: it's a pointer that gets passed instead. This completely eliminates the original issue of reference binding and makes the code compile regardless of the lvalue-ness or rvalue-ness of the array.</p> <p>For example, you can try printing <code>sizeof str</code> inside your function and observe the difference between the two versions. The original will print your array size in bytes (<code>6</code>), while the new one will print pointer size (<code>4</code> for ideone's platform).</p>
 

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