Why do you need to specify the type of the extern / static variable during initialization?

I do not understand whether to specify the type of extern / static variable during initialization. For instance:

struct Test{ static int i; }; Test::i = 2; //error int Test::i = 2; //ok 

Doesn't the compiler know that I have an int type? Is this just a feature of compilers or why is the type specification int int necessary?

+6
source share
4 answers

I do not understand the need to specify the type of external / static variable during initialization.

Because the language developers decided to use the same syntax for declarations and definitions of variables. This syntax includes a type name. You are correct that in some cases this type name is redundant. But letting you leave this can be a little confusing: it will look like a task, not a definition.

Doesn't the compiler know that I have an int type?

Only if the variable is already declared. It should be for a static member like this, but not required for a global variable. You can declare it in one source file:

 extern int i; 

and define it in another

 int i = 42; 

without making the announcement available for determination.

+3
source

This is a basic syntax issue.

A C ++ file consists of a series of declarations. The basic syntax for declaring a variable is something like:

type identifier initializer opt ;

I leave a lot of details, but the main idea is: what should be the declaration in the file area, and the variable declaration should start with the type name.

Without a type name, at the beginning you have a simple assignment operator, but this is only allowed inside functions, not in the file area, as you tried to do it here.

+1
source

This โ€œrestrictionโ€ can be reduced to a simple grammar: statement

 Test::i = 2; //error 

- An operator expression consisting of an assignment expression. This is never parsed as a declaration, no matter what Test::i object might be, and setting up a grammar to cover it would be extremely complex and not be of much use.

+1
source

Here is an example of a valid, executable code that would become ambiguous if the type were not specified in the definition of an integer N::X::i , since the use of dis-mixing structs is possible for structures, but there is nothing equivalent, make the definition int X::i do not match a struct according to your suggested syntax.

 #include <iostream> namespace N { struct X { static int i; struct i { int n_; }; }; } int n = 2; namespace N { int X::i (n); // take int out and under current C++ rules // it will be equivalent to the next line // (which is equivalent to struct X::in; btw) struct X::i (n); // but under your proposed rules, ambiguous } int main() { std::cout << N::n.n_ << ' ' << N::X::i << '\n'; } 

This is a bit confusing, but the bottom line: without the types mentioned, you can break the material.

Other answers basically come down to consistency with variable definitions that appear in other situations - a desirable but not reliable technical driver to decide on the simplification proposed in the question. Of course, this may not be enough to simplify, or it may be regarded as rather obfuscation, while not allowing a serious consideration of Standardization.

+1
source

Source: https://habr.com/ru/post/985359/


All Articles