Re: Linkage of namespace scope reference
Alf P. Steinbach wrote:
One would expect, and I did originally expect, that when initialization
is required, internal linkage is implied, but this special case breaks
that expectation -- for no good reason that I can see.
I don't think I understand why "one would expect" any relationship
between the linkage of a name and whether an explicit initializer is
required where the name is defined. Consider:
struct foo1 { };
struct foo2 { foo2() {} };
namespace ns {
int a; // external linkage; legal
static int b; // internal linkage; legal
const int c; // internal linkage; illegal
int& d; // external linkage; illegal
const int& e; // internal linkage; illegal
const foo1 f1; // internal linkage; illegal
const foo2 f2; // internal linkage; legal
}
All of the above are definitions.
Neither a nor b requires an explicit initializer, even though a has
external linkage and b has internal linkage. c and e have internal
linkage and require initializers, but d (with external linkage) also
requires an initializer. Finally, both f1 and f2 have internal
linkage, but only f1 requires an initializer.
The extern keyword doesn't change whether a definition requires an
initializer; it only changes whether a declaration without an
initializer is also an (ill-formed) definition.
---
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:std-c++@ncar.ucar.edu ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]