class point { public: register int x; //Выдает ошибку "storage class specified for 'x'" }; 

It produces the same error if you apply to the x variable class specifiers of the memory auto , extern . But with static everything is fine. I was looking for the cause on the Internet, but I did not find it. Can you explain to me why this is happening?

  • auto has already changed its meaning. extern affects linking, register is also not used anymore. It is to me that even if it worked earlier, now it would have fallen off anyway. - Croessmah
  • Well, firstly, register has long been excluded from the language. And auto has long lost its original meaning and acquired a new, not connected with it. - AnT

2 answers 2

  1. register is an indication of the storage of a variable in the processor register. How are you going to store a long-lived class field there?

  2. auto - to C ++ 11 the same. After - it is indicated instead of type and instructs the compiler to output this type itself, based on the type of the value assigned. And you do not have this class in the declaration of the class field.

  3. extern indicates that this entity is implemented in another .cpp file. In the case of functions, this is meaningless (they have prototypes for this). In the case of a class, this is doubly pointless (a class is already one large prototype of methods).

  4. And static works because it acquires a completely different meaning. Outside the class, this is a ban on exporting a function from a .cpp file. In the class, this keyword instructs not to use implicit this in the prototype of this function.

    You have completely different concepts mixed together: storage class, linkage type and type deduction.

    The former storage class register has been permanently removed from the language. The word remains key, but not used. In any case, this storage class only makes sense in the definition of an object. A class field declaration is not a definition. Moreover, C ++ initially allowed the use of this storage class only with "short-lived", i.e. local objects. (The same applies to the auto keyword in its original meaning.)

    The extern keyword is primarily intended for managing linkage and it makes sense to specify it only when referring to entities that live in the namespace scope, that is, in practical terms, entities that can generate exported / imported characters in the object file — global variables and functions. A nonstatic class field is not.

    The auto keyword in C ++ is no longer the storage class specifier, but denotes the deductible type in the declaration. Deduction of types for non-static class fields in C ++ is not supported.

    The static in the context of declaring a class member has its own special meaning, which has little to do with the meaning of this keyword in other contexts. It is for this special meaning that it is supported in class member declarations.