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 

static inside a namespace? FAQ?

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





PostPosted: Mon Oct 27, 2003 4:11 pm    Post subject: static inside a namespace? FAQ? Reply with quote



Hello!

I'm working on an open source project, that shall remain nameless. Smile
I've noticed, in the C++ source code, instances where people have
defined "static" variables and functions inside a namespace, sometimes
an anonymous namespace.

namespace Foobar
{
static long wombat_count;
};

Does "static" actually have any value in these contexts? Or is this
just some C programmer's misunderstand of namespaces?

I searched various FAQs for this question, but didn't find it. If this
is in a FAQ somewhere that I missed, a pointer to it would be
appreciated.

TIA!

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





PostPosted: Tue Oct 28, 2003 2:12 am    Post subject: Re: static inside a namespace? FAQ? Reply with quote



In article <f1f66b8d.0310262014.130a9778 (AT) posting (DOT) google.com>,
[email]nu2beam (AT) yahoo (DOT) com[/email] wrote:
Quote:
Hello!

I'm working on an open source project, that shall remain nameless. Smile
I've noticed, in the C++ source code, instances where people have
defined "static" variables and functions inside a namespace, sometimes
an anonymous namespace.

namespace Foobar
{
static long wombat_count;
};

The second semi-colon there is illegal.

Quote:
Does "static" actually have any value in these contexts?

In a named namespace, yes, it gives wombat_count internal linkage,
which means among other things that it is distinct from a
Foobar::wombat_count declared in any other translation unit.
However, given the availability of anonymous namespaces, I don't
think are any good reasons to do this.

Quote:
Or is this just some C programmer's misunderstand of namespaces?

Probably. While it's not totally unreasonable in a named namespace,
using 'static' at namespace scope in an anonymous namespace is
pointless.

Quote:
I searched various FAQs for this question, but didn't find it. If this
is in a FAQ somewhere that I missed, a pointer to it would be
appreciated.

I don't think this specific case is a FAQ.

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

Back to top
Alf P. Steinbach
Guest





PostPosted: Tue Oct 28, 2003 2:13 am    Post subject: Re: static inside a namespace? FAQ? Reply with quote



On 27 Oct 2003 11:11:55 -0500, [email]nu2beam (AT) yahoo (DOT) com[/email] wrote:

Quote:
I'm working on an open source project, that shall remain nameless. Smile
I've noticed, in the C++ source code, instances where people have
defined "static" variables and functions inside a namespace, sometimes
an anonymous namespace.

namespace Foobar
{
static long wombat_count;
};

Does "static" actually have any value in these contexts?

Not in an anonymous namespace.

In a named namespace it has the usual (deprecated) meaning, namely to
give the item internal linkage so different implementation files can
define items of the same name in this namespace.

However, linker conflicts can also be avoided by using an anonymous
namespace, which in contrast to 'static' allows a function to be used
as a template argument.

I prefer 'static' because the specification is textually together with
the item in question.

A purist would have no option but to use an anonymous namespace.


Quote:
Or is this
just some C programmer's misunderstand of namespaces?

In an anonymous namespace, yes; in a named namespace, no.


Quote:
I searched various FAQs for this question, but didn't find it. If this
is in a FAQ somewhere that I missed, a pointer to it would be
appreciated.

AFAIK it's not in the FAQ.


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

Back to top
Jeff King
Guest





PostPosted: Wed Oct 29, 2003 1:40 am    Post subject: Re: static inside a namespace? FAQ? Reply with quote

Alf P. Steinbach <alfps (AT) start (DOT) no> wrote:
Quote:
Does "static" actually have any value in these contexts?

Not in an anonymous namespace.

In a named namespace it has the usual (deprecated) meaning, namely to
give the item internal linkage so different implementation files can
define items of the same name in this namespace.

However, linker conflicts can also be avoided by using an anonymous
namespace, which in contrast to 'static' allows a function to be used
as a template argument.

While I mostly agree, your argument neglects other possible uses of
static. For example, optimizations might be possible at compile or link
time with a static variable that aren't with a global. Such
optimizations should be possible with an anonymous namespace, as well,
but do current compilers necessarily treat anonymously namespaced
variables as static (I can't think of a reason not to, since they should
be inaccessible outside the TU).

For example, the following code:

$ cat foo.cc
namespace { int anonymous_variable; }
namespace ns { int ns_variable; }
static int static_variable;
int regular_variable;

when compiled with g++ 3.3.2 produces:
$ g++ -c foo.cc
$ nm foo.o
00000000 B _ZN23_GLOBAL__N_foo.ccEvV75c18anonymous_variableE
00000004 B _ZN2ns11ns_variableE
0000000c B regular_variable
00000008 b static_variable
$ nm foo.o | c++filt
00000000 B (anonymous namespace)::anonymous_variable
00000004 B ns::ns_variable
0000000c B regular_variable
00000008 b static_variable

Note that the anonymous variable has a unique name, but global linkage.
The static variable has local linkage.

-Peff

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

Back to top
Bo Persson
Guest





PostPosted: Wed Oct 29, 2003 9:31 am    Post subject: Re: static inside a namespace? FAQ? Reply with quote


"Alf P. Steinbach" <alfps (AT) start (DOT) no> skrev i meddelandet
news:3f9d4546.243695734 (AT) News (DOT) CIS.DFN.DE...
Quote:
On 27 Oct 2003 11:11:55 -0500, [email]nu2beam (AT) yahoo (DOT) com[/email] wrote:

I'm working on an open source project, that shall remain nameless.
Smile
I've noticed, in the C++ source code, instances where people have
defined "static" variables and functions inside a namespace,
sometimes
an anonymous namespace.

namespace Foobar
{
static long wombat_count;
};

Does "static" actually have any value in these contexts?

Not in an anonymous namespace.

In a named namespace it has the usual (deprecated) meaning, namely
to
give the item internal linkage so different implementation files can
define items of the same name in this namespace.

However, linker conflicts can also be avoided by using an anonymous
namespace, which in contrast to 'static' allows a function to be
used
as a template argument.

I prefer 'static' because the specification is textually together
with
the item in question.

The you could just write it:

namespace { long wombat_count; }


Quote:

A purist would have no option but to use an anonymous namespace.

Right.


Bo Persson


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