Google Groupes n'accepte plus les nouveaux posts ni abonnements Usenet. Les contenus de l'historique resteront visibles.

Anonymous Namespace or Static

331 vues
Accéder directement au premier message non lu

gaura...@gmail.com

non lue,
30 sept. 2008, 15:02:3930/09/2008
à
Hi,

I am little confused about the use of anonymous namespace over
traditional static keyword for restricting its use in within a
translation unit. I read that names in anonymous namespace have
external linkage as opposed to internal linkage by static keyword.
However since the name cannot be used anyway outside the unit how does
it matter.

Can anyone please help?

Thanks.

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

Damian 'legion' Szuberski

non lue,
30 sept. 2008, 20:31:4930/09/2008
à
On 2008-09-30, gaura...@gmail.com wrote:
> I am little confused about the use of anonymous namespace over
> traditional static keyword for restricting its use in within a
> translation unit. I read that names in anonymous namespace have
> external linkage as opposed to internal linkage by static keyword.
> However since the name cannot be used anyway outside the unit how does
> it matter.
anon. namespace - no external linkage
static - as above but obsolete

--
Damian Szuberski

Joshua...@gmail.com

non lue,
1 oct. 2008, 14:29:0201/10/2008
à
On Sep 30, 5:31 pm, "Damian 'legion' Szuberski"
<leg...@wmid.cutthisjunk.amu.edu.andthis.pl> wrote:

> On 2008-09-30, gauravbj...@gmail.com wrote:
> > I am little confused about the use of anonymous namespace over
> > traditional static keyword for restricting its use in within a
> > translation unit. I read that names in anonymous namespace have
> > external linkage as opposed to internal linkage by static keyword.
> > However since the name cannot be used anyway outside the unit how does
> > it matter.
>
> anon. namespace - no external linkage
> static - as above but obsolete

Incorrect. Members of an anonymous namespace do have external linkage.
Members of anonymous namespaces are also gauranteed to have names
which will not conflict with any other name in any other translation
unit. This is important because arguments to templates generally have
to have external linkage.

Also, static only fixed name collisions of functions, not other
namespace members like classes.


--

Marco Manfredini

non lue,
1 oct. 2008, 14:34:5801/10/2008
à
gaura...@gmail.com wrote:
> Hi,
>
> I am little confused about the use of anonymous namespace over
> traditional static keyword for restricting its use in within a
> translation unit. I read that names in anonymous namespace have
> external linkage as opposed to internal linkage by static keyword.
> However since the name cannot be used anyway outside the unit how does
> it matter.

It matters because names with external linkage can be used in places
where names with internal linkage cannot be used, for example as
template parameters:

namespace
{
void foo() {}
}
static void bar() {}

template<void fn()>
struct X{};

int main()
{
X<foo> x; // OK
X<bar> y; // not OK, bar has internal linkage

Maciej Sobczak

non lue,
1 oct. 2008, 14:39:5801/10/2008
à
On 1 Paďż˝, 02:31, "Damian 'legion' Szuberski"

> anon. namespace - no external linkage

This is not true. Entities in the unnamed namespace can have external
linkage. The only thing is that they cannot be named from outside of
the same translation unit.

Does it matter - technically - in what way is the given name "hidden"
from the outside?
Not really, but the big advantage of unnamed namespaces is that they
communicate the intent more clearly.
They are also easier to use - you can enclose a set of declarations in
an unnamed namespace without modifying those declarations
individually.
The static keyword is too much overloaded and introduces clutter.

--
Maciej Sobczak * www.msobczak.com * www.inspirel.com

The C++ Database Access Library: http://soci.sourceforge.net

rado

non lue,
1 oct. 2008, 14:51:5001/10/2008
à
On Sep 30, 5:31 pm, "Damian 'legion' Szuberski"
<leg...@wmid.cutthisjunk.amu.edu.andthis.pl> wrote:
> On 2008-09-30, gauravbj...@gmail.com wrote:
> > I am little confused about the use of anonymous namespace over
> > traditional static keyword for restricting its use in within a
> > translation unit. I read that names in anonymous namespace have
> > external linkage as opposed to internal linkage by static keyword.
> > However since the name cannot be used anyway outside the unit how does
> > it matter.
>
> anon. namespace - no external linkage
> static - as above but obsolete

{ signature and banner removed -- please don't quote them. -mod }


I think it is:

anon.namespace - unique namespace AND external linkage
static - no extenal linkage

I tend to use 'static' when I have a choice since it is more local:
think about using single keyword compared to matched namespace braces,
and what about the added indentation? The static linkage comes as
additional bonus - although it probably rarely makes a noticeable
difference.
I also found that some compilers (e.g. msvc2005) sometimes choke on
anonymous namespaces but never on 'static'.

Radoslav Getov


--

Richard Corden

non lue,
1 oct. 2008, 15:29:0201/10/2008
à
Damian 'legion' Szuberski wrote:
> On 2008-09-30, gaura...@gmail.com wrote:
>> I am little confused about the use of anonymous namespace over
>> traditional static keyword for restricting its use in within a
>> translation unit.

[...]

> anon. namespace - no external linkage

Actually - it's more like. "External Linkage" but with a name which
cannot be referred to from outside of the TU.


--
Richard Corden

Martin Bonner

non lue,
1 oct. 2008, 15:29:0301/10/2008
à
On Oct 1, 1:31 am, "Damian 'legion' Szuberski"
<leg...@wmid.cutthisjunk.amu.edu.andthis.pl> wrote:

> On 2008-09-30, gauravbj...@gmail.com wrote:
> > I am little confused about the use of anonymous namespace over
> > traditional static keyword for restricting its use in within a
> > translation unit. I read that names in anonymous namespace have
> > external linkage as opposed to internal linkage by static keyword.
> > However since the name cannot be used anyway outside the unit how does
> > it matter.
>
> anon. namespace - no external linkage
> static - as above but obsolete

Actually not so. Stuff in an anonymous namespace /does/ have external
linkage - it just can't be linked to by anything external. (The point
is that "external linkage" has a very specific meaning in the context
of the C++ language. It doesn't just have it's normal Computer
Science meaning.)

The OP astutely asked "how does it matter". The answer is that non-
type arguments to templates must have external linkage - so an object
in an anonymous namespace is fine, but a static object is not.

It is true that "static" was deprecated (in this sense) in the C++98
standard - but it isn't going away anytime soon (I believe it will be
in C++0X, so that means it can't disappear until 2020 at the earliest.)

--

0 nouveau message