Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why aren't static data members allowed in local classes?

What is the reasoning to why static const members cannot exist in local classes? It seems like a rather silly restriction.

Example:

void foo() {   struct bar {     int baz() { return 0; }   // allowed     static const int qux = 0; // not allowed?!?   }; }  struct non_local_bar {   int baz() { return 0; }   // allowed   static const int qux = 0; // allowed }; 

Quote from standard (9.8.4):

A local class shall not have static data members.

like image 737
Pubby Avatar asked Nov 17 '11 06:11

Pubby


People also ask

Can local class have static members?

Local classes are similar to inner classes because they cannot define or declare any static members.

Why static members are defined outside the class?

They are defined outside the class because they are stored seperately rather than as part of objects . Since they are associated with the class and not objects their memory needs to be allocated seperately. Static members belong to the class itself rather than an instance of the class.

Can static data members be declared within the scope of a class?

It is possible to declare a data member of a class as static irrespective of it being a public or a private type in class definition. If a data is declared as static, then the static data is created and initialized only once.

Do static data members belong to all instances of a class?

Two common uses of static fields are to keep a count of the number of objects that have been instantiated, or to store a value that must be shared among all instances. Static methods can be overloaded but not overridden, because they belong to the class, and not to any instance of the class.


2 Answers

From the standard section 9.4.2:

If a static data member is of const integral or const enumeration type, its declaration in the class definition can specify a constant-initializer which shall be an integral constant expression. In that case, the member can appear in integral constant expressions within its scope. The member shall still be defined in a namespace scope if it is used in the program and the namespace scope definition shall not contain an initializer.

Basically, local classes have no linkage, and static data members require a linkage.

Since there's no way to define a static data member of a local class in namespace scope (a declaration with an initializer is not a definition), they are not allowed, whether they are of const integral type or not. On the surface it may seem like the compiler should just be able to inline the value, but then what happens if you try to access a pointer to the member? With namespace scoped classes you'd just get a linker error, but local classes have no linkage.

I guess in theory they could just allow you to use static const integral types in local classes as long as they are only used in integral constant expressions, but it would probably just put too much of a burden on the standards body and compiler vendors to differentiate for very little practical value; local static variables are accessible from local classes, so using a local static const should be just as good.

like image 150
Gerald Avatar answered Oct 03 '22 19:10

Gerald


I dont think there is a.reason. Normal static datamembers are disallowed because there is no way to define them after being declared.

Also dont forget you can create a local const variable outside the.class that you can use inside the class as long as you only read its value (that is, as long as you dont take.its.address).

like image 28
Johannes Schaub - litb Avatar answered Oct 03 '22 19:10

Johannes Schaub - litb