Re: Linkage of namespace scope reference
"Alf P. Steinbach" wrote:
* If a reference has external linkage because it's explicitly
declared 'extern', the initializer can be omitted (?8.5.3/3, "The
initializer can be omitted only ... where the extern specifier is
* If, OTOH., a reference has external linkage because it doesn't
refer to const and hasn't been declared 'static', the initializer
cannot be omitted.
So the requirement to initialize locally hinges not on the
effective linkage of the reference, i.e. on whether the intent
is to initialize it once, in some translation unit, but on
precisely how it acquired external linkage, which, as I see
it, doesn't matter at all wrt. safety or correctness or any
sound criterion for a local initialization requirement, and is
inconsistent with the requirements for other types.
Not really. You're missing another important point: which
declarations are in fact definitions. A reference at namespace
scope which is not declared extern (or a reference with an
initialization, even if it is declared extern) is a definition.
There can only be one definition of the reference in the entire
program (or else you have undefined behavior -- and typically,
an error from the linker), and that definition must have an
I don't think I missed that point. That was my point. It's a peculiar,
seemingly arbitrary special case for references. The way a not
explicitly "extern" reference declaration is a definition is similar to
how a not explicitly "extern" constant declaration is a definition. But
the way such a reference can have external linkage by default is
different from constants.
At namespace scope, we have
T& r = o; // External linkage, definition because of type.
T* const p = &o; // Internal linkage, definition because of type.
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.
Perhaps the best approach is to view constants as the special case.
Or, come to think of it, to view both references and constants as
special cases, which makes ordinary variables a third special case, so
that all we have is a collection of special cases...
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
Q: What is the most annoying thing on usenet and in e-mail?
[ comp.std.c++ is moderated. To submit articles, try just posting with ]
[ your news-reader. If that fails, use mailto:firstname.lastname@example.org ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]