Note that there are some explanatory texts on larger screens.

plurals
  1. POWhat am I allowed to do with a static, constexpr, in-class initialized data member?
    primarykey
    data
    text
    <p>This is probably a bit of an unusual question, in that it asks for a fuller explanation of a short answer given to <a href="https://stackoverflow.com/questions/14547370/confusion-about-in-class-initialization-of-static-data-members/14547498#comment20292860_14547498">another question</a> and of some aspects of the C++11 Standard related to it. </p> <p>For ease of reference, I shall sum up the referenced question here. The OP defines a class:</p> <pre><code>struct Account { static constexpr int period = 30; void foo(const int &amp;) { } void bar() { foo(period); } //no error? }; </code></pre> <p>and is wondering why he gets no error about his usage of an in-class initialized static data member (a book mentioned this to be illegal). Johannes Schaub's answer states, that:</p> <ol> <li>This violates the <strong>One Definition Rule</strong>;</li> <li>No diagnostics is required.</li> </ol> <p>As much as I rely the source and validity of this answer, I honestly dislike it because I personally find it too cryptic, so I tried to work out a more meaningful answer myself, with only partial success. Relevant seems to be § 9.4.2/4: </p> <p><em>"There shall be exactly one definition of a static data member that is <strong>odr-used</strong> (3.2) in a program; <strong>no diagnostic is required</strong>"</em> [Emphases are mine]</p> <p>Which gets me a bit closer to the point. And this is how § 3.2/2 defines an <strong>odr-used</strong> variable:</p> <p><em>"A variable whose name appears as a potentially-evaluated expression is odr-used <strong>unless</strong> it is an object that satisfies the requirements for appearing in a constant expression (5.19) <strong>and</strong> the lvalue-to-rvalue conversion (4.1) is immediately applied"</em> [Emphases are mine]</p> <p>In the OP's question, variable <code>period</code> clearly satisfies the requirements for appearing in a constant expression, being a <code>constexpr</code> variable. So the reason must be certainly found in the <em>second</em> condition: <em>"<strong>and the lvalue-to-rvalue conversion (4.1) is immediately applied</strong>"</em>.</p> <p>This is where I have troubles interpreting the Standard. <strong>What does this second condition actually mean?</strong> What are the situations it covers? Does it mean that a static <code>constexpr</code> variable is <strong>not</strong> odr-used (and therefore can be in-class initialized) if it is returned from a function? </p> <p>More generally: <strong>What are you allowed to do with a static <code>constexpr</code> variable so that you can in-class initialize it?</strong></p>
    singulars
    1. This table or related slice is empty.
    plurals
    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