Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Assuming <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2116.pdf" rel="nofollow noreferrer">N2116</a> is the wording that gets incorporated, then your example is ill-formed (notice that there is no concept of "legal/illegal" in C++). The proposed text for [decl.constexpr]/3 says</p> <blockquote> <ul> <li>its function-body shall be a compound-statement of the form <code>{ return expression; }</code> where expression is a potential constant expression (5.19); </li> </ul> </blockquote> <p>Your function violates the requirement in that it also declares a local variable.</p> <p><strong>Edit</strong>: This restriction could be overcome by moving num outside of the function. The function still wouldn't be well-formed, then, because expression needs to be a potential constant expression, which is defined as</p> <blockquote> <p>An expression is a potential constant expression if it is a constant expression when all occurrences of function parameters are replaced by arbitrary constant expressions of the appropriate type.</p> </blockquote> <p>IOW, <code>reinterpret_cast&lt;const unsigned char*&gt; (&amp;num)[0] == 0xDD</code> would have to be a constant expression. However, it is not: <code>&amp;num</code> would be a address constant-expression (5.19/4). Accessing the value of such a pointer is, however, not allowed for a constant expression:</p> <blockquote> <p>The subscripting operator [] and the class member access . and operators, the <code>&amp;</code> and <code>*</code> unary operators, and pointer casts (except dynamic_casts, 5.2.7) can be used in the creation of an address constant expression, but the value of an object shall not be accessed by the use of these operators.</p> </blockquote> <p><strong>Edit</strong>: The above text is from C++98. Apparently, C++0x is more permissive what it allows for constant expressions. The expression involves an lvalue-to-rvalue conversion of the array reference, which is banned from constant expressions unless</p> <blockquote> <p>it is applied to an lvalue of effective integral type that refers to a non-volatile const variable or static data member initialized with constant expressions </p> </blockquote> <p>It's not clear to me whether <code>(&amp;num)[0]</code> "refers to" a const variable, or whether only a literal <code>num</code> "refers to" such a variable. If <code>(&amp;num)[0]</code> refers to that variable, it is then unclear whether <code>reinterpret_cast&lt;const unsigned char*&gt; (&amp;num)[0]</code> still "refers to" <code>num</code>.</p>
    singulars
    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