Re: Question about Unnamed Namespace
On Oct 15, 5:09 pm, CrazyJohn <gatesgates2...@gmail.com> wrote:
> Dude, thanks for your reply, I really appreciate it. I was just about
> to tell myself I understand this, but what you said confuses me
> again.
> > That's not quite true. What it means is that they cannot be
> > names in other TU's. If your compiler supports export, they can
> > be seen and used by template instantiations, when the template
> > has been defined in another TU. (That is, in fact, part of the
> > reason why the anonymous namespace was invented.)
> > Note for example:
> > template< int& ri > class T{} ;
> > static int i1 ;
> > T< i1 > t1 ; // Illegal...
> > namespace { int i2 ; } ;
> > T< i2 > t2 ; // Legal...
> I understand why using "i1" is illegal, but would you explain why
> using unnamed namespace is legal again?
The difference is simple: using static forces internal linkage,
and the standard says that you cannot instantiate a template
over something that has internal linkage. Roughly speaking, the
entity being named cannot be accessed outside of the translation
unit (and the "translation unit" in which template instantiation
takes place is distinct from the one which triggers the
instantiation, at least when the template is exported). An
anonymous namespace has no impact on linkage. The entity has
external linkage, exactly as if it had been declared in a named
namespace, or globally. What protects it from accidental
conflicts in other translation units is that you can't name the
namespace it's in. You can't name it, but the compiler still
knows, and can use it in the instantiation of a template.
> And what is this "export"
> thing btw......
A standard (but not widely implemented) feature which allows you
to define the templates in a separate translation unit, so that
you don't accidentally pick up unwanted local definitions.
--
James Kanze (GABI Software) email:james.kanze@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34
|