Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>[<em>Note: Originally, this was not meant to be a self-answered question; I just happened to find the answer myself while I was describing my attempts to investigate, and I thought it would have been nice to share it.</em>]</p> <p>According to Annex C (2.14.5) of the C++11 Standard:</p> <blockquote> <p>The type of a string literal is changed from “array of char” to “<strong>array of const char</strong>.” [....]</p> </blockquote> <p>Moreover, Paragraph 7.1.6.2/4 specifies (about the result of <code>decltype</code>):</p> <blockquote> <p>The type denoted by <code>decltype(e)</code> is defined as follows:</p> <p>— if <code>e</code> is an unparenthesized id-expression or an unparenthesized class member access (5.2.5), <code>decltype(e)</code> is the type of the entity named by <code>e</code>. If there is no such entity, or if <code>e</code> names a set of overloaded functions, the program is ill-formed;</p> <p>— otherwise, if <code>e</code> is an xvalue, <code>decltype(e)</code> is <code>T&amp;&amp;</code>, where <code>T</code> is the type of <code>e</code>;</p> <p>— <strong>otherwise, if <code>e</code> is an lvalue, <code>decltype(e)</code> is <code>T&amp;</code>, where <code>T</code> is the type of <code>e</code></strong>;</p> <p>— otherwise, <code>decltype(e)</code> is the type of <code>e</code>.</p> </blockquote> <p>Since <strong><a href="https://stackoverflow.com/questions/15036281/c11-value-category-of-string-literal/15036305#15036305">string literals are lvalues</a></strong>, according to the above Paragraph and the Paragraph from Annex C, the result of <code>decltype("Hello")</code> is an <strong><em>lvalue reference to</em></strong> an array of size 6 of constant narrow characters:</p> <pre><code>#include &lt;type_traits&gt; int main() { // This will NOT fire static_assert( std::is_same&lt;decltype("Hello"), char const (&amp;)[6]&gt;::value, "Error!" ); } </code></pre> <p>Finally, even though the <code>hello</code> variable <em>is</em> also an lvalue, the second compile-time assertion from the question's text does not fire, because <code>hello</code> is an <em>unparenthesized id-expression</em>, which makes it fall into the first item of the above list from Paragraph 7.1.6.2/4. Therefore, the result of <code>decltype(hello)</code> is the type of the entity named by <code>hello</code>, which is <code>char const[6]</code>.</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