473,387 Members | 1,812 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Anonymous namespace vs static

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?

--

Oct 1 '08 #1
5 3877
ga*********@gmail.com wrote:
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?
You're correct, the only potential downside is the increased number of
external names in every module that contains objects and functions in an
unnamed namespace. The linker is unlikely to run out of memory while
processing them, however, I don't think you'd notice anything. The size
of the object module should only affect the speed at which the linker
can output the resulting executable. As you suggest, most of the modern
linkers are smart enough to remove parts of the executable that are not
used (or exported, in a dynamic library). Just out of curiosity, how
big is your project?

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 1 '08 #2
Hi,

ga*********@gmail.com schrieb:
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
Oct 1 '08 #3
Marcel Müller wrote:
ga*********@gmail.com schrieb:
>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?
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 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.
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.

Oct 1 '08 #4
Rolf Magnus wrote:
[..]
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.
The creators of linkers probably know. If I were asked to speculate,
I'd probably say that the linker can utilise "near" addressing for
symbols without external linkage and is forced to use "far" addressing
for symbols with external linkage. It might have to play into the
instructions the compiler creates for loading those addresses and
passing them as arguments when calling external functions...

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Oct 1 '08 #5
On Oct 1, 7:05 pm, Rolf Magnus <ramag...@t-online.dewrote:

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

--
James Kanze (GABI Software) email:ja*********@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
Oct 1 '08 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tron Thomas | last post by:
I have read that anonymous namespaces are preferred in C++ over declaring global statics because the namespace can hide information from other translation units and at the same time provide...
7
by: George Carl | last post by:
Perhaps this is a dumb question, but I've tried Google and the newsgroups, and can't figure out a satisfactory answer yet I'd like to make publish/subscribe scenario where the publishing object is...
7
by: d225563 | last post by:
I read an article that had a really elegant solution to pass parameters to a thread by using an anonymous method as your ThreadStart. It seemed pretty slick and even worked when I tried it. ...
3
by: Frederick Gotham | last post by:
Back in the day, if you wanted a function to be self-contained within a translation unit, you defined the function as "static". If there were an external linkage function by the same name...
7
by: Bill Woodruff | last post by:
I've found it's no problem to insert instances of named delegates as values into a generic dictionary of the form : private Dictionary<KeyType, DelegatemyDictionary = new Dictionary<KeyType,...
6
by: Gaijinco | last post by:
I have always felt that there are a lot of topics that you learned the facts but you only grasp the matter sometime down the road. For me, two of those topics are inner classes and anonymous...
1
by: ktrvnbq02 | last post by:
Hi, I recently came to debug some old code that was causing a StackOverflowException. The code in question makes significant use of recursion and with large data structures it exhausted the...
12
by: Taras_96 | last post by:
Hi everyone, AFAIK external linkage allows you to refer to variables/functions outside of the current translation unit. A variable in an unnamed namespace is similar to declaring a static...
22
by: Luna Moon | last post by:
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...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.