James Kanze
10/23/2008 8:15:00 PM
On Oct 23, 5:35 pm, "spamboun...@gmx.de" <spamboun...@gmx.de> wrote:
> > ... A template can only be instantiated over something that
> > has external linkage, e.g.:
> > template< int* p > class T {} ;
> > static int i ;
> > namespace {
> > int j ;
> > }
> > T< &i > ti ; // illegal...
> > T< &j > tj ; // legal.
> To me, anonymous namespaces were just the C++ way of saying
> static. The external linkage point is new to me. But:
> Pointer-to-int as template argument?
Why not? It was the simplest example that came to my mind.
> Instantiating templates over something with external linkage?
Anything you use to instantiate a template must have external
linkage. Those are the rules.
> Could you provide a practical example working with anonymous
> namespace but not working with static?
I just did. See above: declare the variable static, and you
can't use it to instantiate the template. Define it in an
anonymous namespace, and you can.
I run into this a lot because of const; const is static by
default, so something like:
template< int* p > class T{} ;
namespace {
int const i = 43 ;
}
T< &i > t ; // illegal.
You have to define the int:
extern int const i = 43 ;
(In practice, of course, most of the time, it's a user defined
class, and the template parameter is a reference, rather than a
pointer, and it's a function template, not a class template.
But the same principles apply.)
> Note that you could also omit the static keyword to the same
> effect.
Except that if I omitted the static keyword in my example, the
symbol would be visible in every module. And conflict with
another module which did exactly the same thing with the same
name.
> > > BTW, the proper term is "unnamed namespace" (C++03 7.3.1.1).
> > > The author of the C++ book you're reading should have taken
> > > the time to get it right.
> > The term anonymous namespace seems pretty widespread to me.
> I'd bet a horse and a carrot that "unnamed" is the literal
> meaning of "anonymous".
Actually, no. From the way I understand English, anonymous
means that the name is hidden, and not visible; unnamed means
that it doesn't exist. In which case, anonymous is actually
closer to what really happens.
--
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