简体   繁体   中英

using a static const int in a struct/class

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?

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?

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.

However, if you try the ternary operand without "defining" static consts, you get a linker error in GCC 4x:

http://gcc.gnu.org/bugzilla/show_bug.cgi?id=13795

So, although constructs like int k = A::CONSTVAL; are illegal in the current standard, they are supported. But the ternary operand is not. Some operators are more equal than others, if you get my drift :)

So much for "lax" rules. I suggest you write code conforming to the standard if you do not want surprises.

In general, most (and recent) C++ compilers allow static const ints

You just lucky, perhaps not. Try older compiler, such as gcc 2.0 and it will vehemently punish you with-less-than-pretty error message.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM