Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>By really pedantic rules, yes, your code needs a definition for that static integer. But by practical rules, and what all compilers implement because that's how the rules of C++03 are intended - no, you don't need a definition. </p> <p>The rules for such static constant integers are intended to allow you to omit the definition if the integer is used only in such situations where a value is immediately read, and if the static member can be used in constant expressions. </p> <p>In your return statement, the value of the member is immediately read, so you can omit the definition of the static constant integer member if that's the only use of it. The following situation needs a definition, however:</p> <pre><code>struct A { static const int a = 5; struct B { static const int b = a; }; }; int main() { int *p = &amp;A::B::b; } </code></pre> <p>No value is read here - but instead the address of it is taken. Therefore, the intent of the C++03 Standard is that you have to provide a definition for the member like the following in some implementation file. </p> <pre><code>const int A::B::b; </code></pre> <p>Note that the <em>actual</em> rules appearing in the C++03 Standard says that a definition is not required only where the variable is used where a constant expression is <em>required</em>. That rule, however, if strictly applied, is too strict. It would only allow you to omit a definition for situation like array-dimensions - but would require a definition in cases like a return statement. The corresponding defect report is <a href="http://www.open-std.org/jtc1/sc22/wg21/docs/cwg_defects.html#454" rel="noreferrer">here</a>. </p> <p>The wording of C++0x has been updated to include that defect report resolution, and to allow your code as written. </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. 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