[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.c++

question about anonymous namespace...

Luna Moon

10/22/2008 10:57:00 PM

I am reading the book "C++ Annotations", and here is a quote from the
book:

Namespaces can be defined without a name. Such a namespace is
anonymous and it restricts the
visibility of the defined entities to the source file in which the
anonymous namespace is defined.
Entities defined in the anonymous namespace are comparable to C’s
static functions and variables.
In C++ the static keyword can still be used, but its use is more
common in class definitions
(see chapter 6). In situations where static variables or functions are
necessary, the use of the
anonymous namespace is preferred.


--------------

Could anybody give me an example about why the anonymous name space is
the same as "static" variables and functions in C?

thanks!
22 Answers

Ian Collins

10/22/2008 11:05:00 PM

0

Luna Moon wrote:
> I am reading the book "C++ Annotations", and here is a quote from the
> book:
>
> Namespaces can be defined without a name. Such a namespace is
> anonymous and it restricts the
> visibility of the defined entities to the source file in which the
> anonymous namespace is defined.
> Entities defined in the anonymous namespace are comparable to C?s
> static functions and variables.
> In C++ the static keyword can still be used, but its use is more
> common in class definitions
> (see chapter 6). In situations where static variables or functions are
> necessary, the use of the
> anonymous namespace is preferred.
>
>
> --------------
>
> Could anybody give me an example about why the anonymous name space is
> the same as "static" variables and functions in C?
>
The quote says it all: "it restricts the visibility of the defined
entities to the source file in which the anonymous namespace is defined"

Which is exactly what "static" does in C.

--
Ian Collins

blargg.h4g

10/23/2008 7:09:00 AM

0

In article
<4b97d33c-17f1-4bf8-a02b-71ca05250278@a29g2000pra.googlegroups.com>, Luna
Moon <lunamoonmoon@gmail.com> wrote:

> I am reading the book "C++ Annotations", and here is a quote from the
> book:
>
> Namespaces can be defined without a name. Such a namespace is
> anonymous and it restricts the visibility of the defined entities
> to the source file in which the anonymous namespace is defined.
> Entities defined in the anonymous namespace are comparable to C's
> static functions and variables. In C++ the static keyword can still
> be used, but its use is more common in class definitions (see
> chapter 6). In situations where static variables or functions are
> necessary, the use of the anonymous namespace is preferred.
>
> --------------
>
> Could anybody give me an example about why the anonymous name space is
> the same as "static" variables and functions in C?

Why? Because the standard says so. Actually, the effect is not exactly the same.

// Before namespace
static int i;
static void f() { }
struct my_foo { };

// After namespace
namespace
{
int i;
void f() { }
struct foo { }; // no need for prefix on name, since it's now a local 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.

James Kanze

10/23/2008 9:10:00 AM

0

On Oct 23, 9:08 am, blargg....@gishpuppy.com (blargg) wrote:
> In article
> <4b97d33c-17f1-4bf8-a02b-71ca05250...@a29g2000pra.googlegroups.com>,
> Luna Moon <lunamoonm...@gmail.com> wrote:

[...]
> > Could anybody give me an example about why the anonymous
> > name space is the same as "static" variables and functions
> > in C?

> Why? Because the standard says so. Actually, the effect is not
> exactly the same.

> // Before namespace
> static int i;
> static void f() { }
> struct my_foo { };

> // After namespace
> namespace
> {
> int i;
> void f() { }
> struct foo { }; // no need for prefix on name, since it's now a local name
> }

Another important difference is the linkage of the name. A
static variable or function has internal linkage. A name in an
anonymous namespace has external linkage---the reason it can't
be seen from other translation units is that it isn't namable in
them. (Formally, it is visible everywhere, but since it can't
be named, there's no way to actually see it.) 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.

> 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.

--
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

spambouncer@gmx.de

10/23/2008 3:35:00 PM

0

> ... 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? Instantiating templates over
something with external linkage? Could you provide a practical example
working with anonymous namespace but not working with static? Note
that you could also omit the static keyword to the same effect.

> > 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".

Best regards,
Andreas.

Michael Mol

10/23/2008 4:24:00 PM

0

On Oct 23, 3:08 am, blargg....@gishpuppy.com (blargg) wrote:

[snip]

> // Before namespace
> static int i;
> static void f() { }
> struct my_foo { };
>
> // After namespace
> namespace
> {
>     int i;
>     void f() { }
>     struct foo { }; // no need for prefix on name, since it's now a local name
>
> }


Now I'm curious. Take the following code from the same compilation
unit:

namespace
{
int i;
void x() { }
}

namespace
{
int j;
void y() { }
}

Would i be visible to y(), and j be visible to x()?

Pete Becker

10/23/2008 4:27:00 PM

0

On 2008-10-23 12:23:54 -0400, Michael Mol <mikemol@gmail.com> said:

> On Oct 23, 3:08 am, blargg....@gishpuppy.com (blargg) wrote:
>
> [snip]
>
>> // Before namespace
>> static int i;
>> static void f() { }
>> struct my_foo { };
>>
>> // After namespace
>> namespace
>> {
>>     int i;
>>     void f() { }
>>     struct foo { }; // no need for prefix on name, since it's now a l
> ocal name
>>
>> }
>
>
> Now I'm curious. Take the following code from the same compilation
> unit:
>
> namespace
> {
> int i;
> void x() { }
> }
>
> namespace
> {
> int j;
> void y() { }
> }
>
> Would i be visible to y(),

yes.

> and j be visible to x()?

no.

Same answer with a named namespace and without a namespace.

--
Pete
Roundhouse Consulting, Ltd. (www.versatilecoding.com) Author of "The
Standard C++ Library Extensions: a Tutorial and Reference
(www.petebecker.com/tr1book)

spambouncer@gmx.de

10/23/2008 6:02:00 PM

0

> Now I'm curious. Take the following code from the same compilation
> unit:
>
> namespace
> {
> int i;
> void x() { }
> }
>
> namespace
> {
> int j;
> void y() { }
> }
>
> Would i be visible to y(), and j be visible to x()?

Generally, namespaces can be split across several compilation units.
The only exception to this rule are anonymous namespaces. If you would
split an anonymous namespace across several compilation units, it
wouldn't be the same namespace. That's because anonymous namespaces
are not truely anonymous, it's just that their name is not accessible
by the coder. The compiler, however, assigns a unique ID to them per
compilation unit.

But, as you're asking for the same compilation unit, yes, i is visible
to y(), and no, j is not visible to x().

Best regards,
Andreas.

blargg.h4g

10/23/2008 7:59:00 PM

0

In article
<340f72bf-6bd6-4d58-8e3a-69d9805e7c71@g61g2000hsf.googlegroups.com>,
Michael Mol <mikemol@gmail.com> wrote:

> On Oct 23, 3:08 am, blargg....@gishpuppy.com (blargg) wrote:
>
> [snip]
>
> > // Before namespace
> > static int i;
> > static void f() { }
> > struct my_foo { };
> >
> > // After namespace
> > namespace
> > {
> > int i;
> > void f() { }
> > struct foo { }; // no need for prefix on name, since it's now a
> > // local name
> >
> > }
>
> Now I'm curious. Take the following code from the same compilation
> unit:
>
> namespace
> {
> int i;
> void x() { }
> }
>
> namespace
> {
> int j;
> void y() { }
> }
>
> Would i be visible to y(), and j be visible to x()?

Heck, since we're talking about the same translation unit, you could also add

// at file scope in same translation unit
void z()
{
i = 0;
j = 0;
x();
y();
}

and it would see everything in your unnamed namespaces (and z would be
visible to other translation units).

James Kanze

10/23/2008 8:15:00 PM

0

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

Hendrik Schober

10/23/2008 8:53:00 PM

0

James Kanze wrote:
> On Oct 23, 5:35 pm, "spamboun...@gmx.de" <spamboun...@gmx.de> wrote:
> [...]
>> 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.

According to my dictionary, "anonymous" derives from an old
Latin word which derives from a Greek word, consisting of "an"
("without") and "onuma" ("name"), and meaning "nameless".
I know, of course, that this leaves a lot of room for a debate
about whether today this actually is "the literal meaning" of
the word or not... However, my dictionary then produces "having
an unknown or unacknowledged name" as the first of the three
meanings it comes up with. (And now there's room for a debate
whether my dictionary is actually worth anything...)

Schobi