C++Talk.NET Forum Index C++Talk.NET
C++ language newsgroups
 
Archives   FAQFAQ   SearchSearch   MemberlistMemberlist   UsergroupsUsergroups   RegisterRegister 
 ProfileProfile   Log in to check your private messagesLog in to check your private messages   Log inLog in 

superiority of unnamed namespaces over static linkage?

 
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated)
View previous topic :: View next topic  
Author Message
bert hubert
Guest





PostPosted: Mon Feb 16, 2004 11:10 pm    Post subject: superiority of unnamed namespaces over static linkage? Reply with quote



I was reading the most excellent hardcover edition of the C++ Standard
( http://www.amazon.co.uk/exec/obidos/ASIN/0470846747 ) where I happened
upon the claim that unnamed namespaces are superior over static linkage.

Besides the fact that an unnamed namespace conveys more succinctly the local
nature of a name, are there any other reaons for this superiority?

In other words, are there semantical differences which are beneficial or
efficiency reasons?

Thanks.

--
http://www.PowerDNS.com/pdns Try our new database driven nameserver!
http://lartc.org Linux Advanced Routing & Traffic Control HOWTO

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]
Back to top
Francis Glassborow
Guest





PostPosted: Tue Feb 17, 2004 2:23 pm    Post subject: Re: superiority of unnamed namespaces over static linkage? Reply with quote



In message <slrnc318bf.de7.ahu (AT) hubert (DOT) ds9a.nl>, bert hubert
<ahu (AT) ds9a (DOT) nl> writes
Quote:
I was reading the most excellent hardcover edition of the C++ Standard
( http://www.amazon.co.uk/exec/obidos/ASIN/0470846747 ) where I happened
upon the claim that unnamed namespaces are superior over static linkage.

Besides the fact that an unnamed namespace conveys more succinctly the local
nature of a name, are there any other reaons for this superiority?

Things like classes cannot be declared static so if you want to hide
them in a TU the unnamed namespace will cover it.

Quote:

In other words, are there semantical differences which are beneficial or
efficiency reasons?

Yes, things in the unnamed namespace have extern linkage and so can be
used where something with internal linkage could not.

Then there is the small issue that a named namespace can contain an
unnamed namespace.


--
Francis Glassborow ACCU
Author of 'You Can Do It!' see http://www.spellen.org/youcandoit
For project ideas and contributions: http://www.spellen.org/youcandoit/projects


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
bert hubert
Guest





PostPosted: Tue Feb 17, 2004 5:12 pm    Post subject: Re: superiority of unnamed namespaces over static linkage? Reply with quote



On 2004-02-17, Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> wrote:
Quote:
Besides the fact that an unnamed namespace conveys more succinctly the local
nature of a name, are there any other reaons for this superiority?

Things like classes cannot be declared static so if you want to hide
them in a TU the unnamed namespace will cover it.

Ahh, I see. I had not previously realised that if there are two files both
defining a class called 'Thing' differently that this will mess up things
greatly at link time.

Quote:
Then there is the small issue that a named namespace can contain an
unnamed namespace.

To create an 'inner unnamed namespace' which does not affect the rest of the
TU? Might be useful in include files containing real code I suspect
(probably templates).

Thanks for your elucidating answer!

Bert.


--
http://www.PowerDNS.com/ Open Source Database Driven Nameserver
http://lartc.org Linux Advanced Routing & Traffic Control HOWTO

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Hendrik Schober
Guest





PostPosted: Wed Feb 18, 2004 1:39 am    Post subject: Re: superiority of unnamed namespaces over static linkage? Reply with quote

bert hubert <ahu (AT) ds9a (DOT) nl> wrote:
Quote:
[...]
Then there is the small issue that a named namespace can contain an
unnamed namespace.

To create an 'inner unnamed namespace' which does not affect the rest of the
TU? Might be useful in include files containing real code I suspect
(probably templates).

I consider using unnamed namespaces in
included files a bug. In each TU the
files get included, all the symbols of
these namespaces will be duplicated --
rarely ever what people want.

Quote:
Thanks for your elucidating answer!

Bert.

Schobi

--
[email]SpamTrap (AT) gmx (DOT) de[/email] is never read
I'm Schobi at suespammers dot org

"Sometimes compilers are so much more reasonable than people."
Scott Meyers



[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Matthew Collett
Guest





PostPosted: Wed Feb 18, 2004 11:16 am    Post subject: Re: superiority of unnamed namespaces over static linkage? Reply with quote

In article <slrnc34b57.eu9.ahu (AT) hubert (DOT) ds9a.nl>,
bert hubert <ahu (AT) ds9a (DOT) nl> wrote:

Quote:
On 2004-02-17, Francis Glassborow <francis (AT) robinton (DOT) demon.co.uk> wrote:
Then there is the small issue that a named namespace can contain an
unnamed namespace.

To create an 'inner unnamed namespace' which does not affect the rest of the
TU? Might be useful in include files containing real code I suspect
(probably templates).

No, that is one thing it _isn't_ useful for. Unnamed namespaces should
only be used in implementation files, not in include files, otherwise
you are defining multiple copies of the things in the namespace.

One reason to nest an unnamed namespace inside a named one is simply to
get name look-up to work the way you want for the 'hidden' code.

Best wishes,
Matthew Collett

--
Those who assert that the mathematical sciences have nothing to say
about the good or the beautiful are mistaken. -- Aristotle


[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Gabriel Dos Reis
Guest





PostPosted: Wed Feb 18, 2004 11:16 pm    Post subject: Re: superiority of unnamed namespaces over static linkage? Reply with quote

bert hubert <ahu (AT) ds9a (DOT) nl> writes:

Quote:
I was reading the most excellent hardcover edition of the C++ Standard
( http://www.amazon.co.uk/exec/obidos/ASIN/0470846747 ) where I happened
upon the claim that unnamed namespaces are superior over static linkage.

Besides the fact that an unnamed namespace conveys more succinctly the local
nature of a name, are there any other reaons for this superiority?

In other words, are there semantical differences which are beneficial or
efficiency reasons?

You've got various answers. I'd point out that "supriority" here
really depends on what you're doing. For example, the "static"
keyword affects the second phase of name lookup (only function names
with external linkage are considered). So, if you're using an
exported template and you really mean your function not be
"reachable"from another translation unit -- e.g. really really
"local" -- then use "static".

--
Gabriel Dos Reis
[email]gdr (AT) integrable-solutions (DOT) net[/email]

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Daveed Vandevoorde
Guest





PostPosted: Thu Feb 19, 2004 10:40 am    Post subject: Re: superiority of unnamed namespaces over static linkage? Reply with quote

bert hubert <ahu (AT) ds9a (DOT) nl> wrote

Quote:
I was reading the most excellent hardcover edition of the C++ Standard
( http://www.amazon.co.uk/exec/obidos/ASIN/0470846747 ) where I happened
upon the claim that unnamed namespaces are superior over static linkage.

I would disagree that one is superior over the other.
They're different ways of making things private to a
translation unit.

Quote:
Besides the fact that an unnamed namespace conveys more succinctly the local
nature of a name, are there any other reaons for this superiority?

In other words, are there semantical differences which are beneficial or
efficiency reasons?

Declarations with internal linkage (i.e., "static") are invisible to phase-2
argument-dependent lookup (ADL). Declarations in unnamed namespaces
have external linkage, which means they may be found through phase-2
ADL. However, since they have external linkage, they may potentially
degrade the performance of the linking process.

Daveed

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
bert hubert
Guest





PostPosted: Fri Feb 20, 2004 1:13 am    Post subject: Re: superiority of unnamed namespaces over static linkage? Reply with quote

On 2004-02-18, Gabriel Dos Reis <gdr (AT) integrable-solutions (DOT) net> wrote:

Quote:
You've got various answers. I'd point out that "supriority" here
really depends on what you're doing. For example, the "static"
keyword affects the second phase of name lookup (only function names
with external linkage are considered). So, if you're using an
exported template and you really mean your function not be
"reachable"from another translation unit -- e.g. really really
"local" -- then use "static".

Thanks to everybody. I now use unnamed namespaces for things that should be
local, and I know why.

--
http://www.PowerDNS.com/ Open Source Database Driven Nameserver
http://lartc.org Linux Advanced Routing & Traffic Control HOWTO

[ See http://www.gotw.ca/resources/clcm.htm for info about ]
[ comp.lang.c++.moderated. First time posters: Do this! ]

Back to top
Display posts from previous:   
Post new topic   Reply to topic    C++Talk.NET Forum Index -> C++ Language (Moderated) All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum


Powered by phpBB © 2001, 2006 phpBB Group
SEO toolkit © 2004-2006 webmedic.