I received a double definition error from the linker in a larger project I'm working on. I was surprised to see that I was allowed to accidentally include an initializer together with extern
.
The following compiles, but I did not expect it to.
extern int var{3}; // declaration or definition?
//int var{3};
int main()
{
int varb = var;
return 0;
}
I would expect extern
to specify that a variable is only being declared, not defined, yet if I add an initializer, does it then become defined, why?
I have looked at the following questions, but they don't explain to me why this is allowed.
The second answer does clarify the following:
a declaration is a definition unless it contains the extern keyword without an initializer or function body
So the specification does indicate that this should then be a definition, but why? Does that mean that the following two lines mean exactly the same thing?
extern int var{3};
int var{3};