Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using a static const int in a struct/class

Tags:

c++

struct A {
    static const int a = 5;

    struct B {
       static const int b = a;
    };

 };

 int main() {
   return A::B::b;
 }

The above code compiles. However if you go by Effective C++ book by Scott Myers(pg 14); We need a definition for a in addition to the declaration. Can anyone explain why this is an exception?

like image 922
Pradyot Avatar asked Aug 21 '09 14:08

Pradyot


2 Answers

C++ compilers allow static const integers (and integers only) to have their value specified at the location they are declared. This is because the variable is essentially not needed, and lives only in the code (it is typically compiled out).

Other variable types (such as static const char*) cannot typically be defined where they are declared, and require a separate definition.

For a tiny bit more explanation, realize that accessing a global variable typically requires making an address reference in the lower-level code. But your global variable is an integer whose size is this typically around the size of an address, and the compiler realizes it will never change, so why bother adding the pointer abstraction?

like image 122
Walt W Avatar answered Oct 23 '22 04:10

Walt W


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.

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.

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:

struct A {
    static const int a = 5;

    struct B {
       static const int b = a;
    };

 };

 int main() {
   int *p = &A::B::b;
 }

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.

const int A::B::b;

Note that the actual 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 required. 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 here.

The wording of C++0x has been updated to include that defect report resolution, and to allow your code as written.

like image 45
Johannes Schaub - litb Avatar answered Oct 23 '22 04:10

Johannes Schaub - litb