Anonymous namespace vs static

G

gauravbjain

I am planning to move from ‘static’ keyword to anonymous namespace for
variables/functions which are local to a translation unit. This is to
move code closer to C++, since static for file level names is
deprecated by standard.
I am queries if doing this may have any downside associated with it,
since it would give external linkage to all these names? May be size
increase of generated object files which I believe should be taken
care by final symbol stripping. Suggestions?

--
 
M

Marcel Müller

Hi,

I am planning to move from ‘static’ keyword to anonymous namespace for
variables/functions which are local to a translation unit. This is to
move code closer to C++, since static for file level names is
deprecated by standard.

static linkage is deprecated?
Well, the second meaning of static has always been a bit confusing for
beginners. But the anonymous namspaces are none the better. The have
also an excemption: the implicit using statement.
I am queries if doing this may have any downside associated with it,
since it would give external linkage to all these names?

I don't think so. Since the namespace has no name, the functions cannot
be exposed by a biunique global symbol name anyway.
May be size
increase of generated object files
which I believe should be taken
care by final symbol stripping.
Suggestions?

Simply do it.


Marcel
 
R

Rolf Magnus

Marcel said:
static linkage is deprecated?
Yes.

Well, the second meaning of static has always been a bit confusing for
beginners.

So this one is the second? Who numbered them?
But the anonymous namspaces are none the better. The have
also an excemption: the implicit using statement.

Not sure what you mean by that.
I don't think so. Since the namespace has no name, the functions cannot
be exposed by a biunique global symbol name anyway.

The standard requires them to have external linkage. And the compiler I use
does actually provide them as external symbols using some unique prefix for
the symbol name. Why it needs to do this, I don't know.
 
J

James Kanze

[re definitions in anonymous namespaces...]
The standard requires them to have external linkage. And the
compiler I use does actually provide them as external symbols
using some unique prefix for the symbol name. Why it needs to
do this, I don't know.

Possibly for its implementation of export:). (Seriously,
probably just because that's the simplest solution. Generate
the unique name, and for the rest, treat it just like anything
else. No special cases, no extra code, etc.)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top