Dan Ganea
Dan Ganea

Reputation: 550

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

I do not understand the need to specify the type of an extern/static variable at initialization. For example:

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

Doesn't the compiler know that i is of type int? Is it just a particularity of the compilers, or why is specification of the type,,int" needed?

Upvotes: 7

Views: 246

Answers (4)

Tony Delroy
Tony Delroy

Reputation: 106126

Here's an example of valid, runnable code that would become ambiguous if the type weren't specified in the definition of the integer N::X::i, given the use of the disambiguating struct is possible for structures but there's nothing equivalent to force the int X::i definition not to match the struct under your proposed 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::i n; btw)
  struct X::i (n); // but under your proposed rules, ambiguous
}

int main()
{
    std::cout << N::n.n_ << ' ' << N::X::i << '\n';
}

That's a bit convoluted, but the bottom line is: not having types mentioned could break stuff.

Other answers basically amount to consistency with variable definitions appearing in other situations - that's desirable but not a sound technical driver to decide against the simplification proposed in the question. Of course, it may not be enough of a simplification, or may be seen as more of an obfuscation, preventing it ever having been seriously considered for Standardisation.

Upvotes: 1

Columbo
Columbo

Reputation: 60999

This "limitation" can be reduced to simple grammar: The statement

Test::i = 2; //error

is an expression-statement consisting of an assignment-expression. This is never actually parsed as a declaration, whatever the entity Test::i may be, and adjusting the grammar to cover this would be extremely complicated and have no great benefit whatsoever.

Upvotes: 1

Jerry Coffin
Jerry Coffin

Reputation: 490218

It's a matter of basic syntax.

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

type identifier initializeropt ;

I'm leaving out a lot of details, but that's the basic idea: what's at file scope has to be a declaration, and a variable declaration has to start with the name of a type.

Without the name of a type on the beginning, you have a simple assignment statement--but those are only allowed inside of functions, not at file scope as you've tried to do it here.

Upvotes: 1

Mike Seymour
Mike Seymour

Reputation: 254501

I do not understand the need to specify the type of an extern/static variable at initialization.

Because the language designers chose to use the same syntax for variable declarations and definitions. That syntax includes the type name. You're correct that, in some cases, that type name is redundant. But allowing you to leave it out might be somewhat confusing: it would look like an assignment, not a definition.

Doesn't the compiler know that i is of type int?

Only if the variable has already been declared. That has to be the case for a static member like this, but not necessarily for a global variable. You could declare it in one source file:

extern int i;

and define it in another

int i = 42;

without making the declaration available to the definition.

Upvotes: 3

Related Questions