473,405 Members | 2,210 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,405 software developers and data experts.

KDE FAQ states using static functions is better than putting themin an anonyous namespace

For years, I've been putting everything that won't be needed outside a
compilation unit in anonymous namespaces, even editing my old files that did
things the 'old' way (using static linkage). Then suddenly I find this in the
KDE developer faq:

Q: 2.24. I put some functions in anonymous namespace and someone reverted it
and made those functions static, why?
Symbols defined in a C++ anonymous namespace do NOT have internal linkage.
Anonymous namespaces only give an unique name for that translation unit and
that is it; they don't change the linkage of the symbol at all. Linkage isn't
changed on those because the second phase of two-phase name lookup ignores
functions with internal linkages. Also, entities with internal linkage cannot
be used as template arguments

Is that true? What are the implications of this? Should I now go around all
my source files and make functions/global variables declared in them static?

--
To reply, take of all ZIGs !!
Feb 4 '06 #1
3 1551
"Asfand Yar Qazi" <ay*******@qaziZIG.f2sZIG.com> wrote in message
news:ds**********@domitilla.aioe.org...
: For years, I've been putting everything that won't be needed outside a
: compilation unit in anonymous namespaces, even editing my old files that
did
: things the 'old' way (using static linkage). Then suddenly I find this
in the
: KDE developer faq:
:
: Q: 2.24. I put some functions in anonymous namespace and someone
reverted it
: and made those functions static, why?
: Symbols defined in a C++ anonymous namespace do NOT have internal
linkage.
: Anonymous namespaces only give an unique name for that translation unit
and
: that is it; they don't change the linkage of the symbol at all. Linkage
isn't
: changed on those because the second phase of two-phase name lookup
ignores
: functions with internal linkages. Also, entities with internal linkage
cannot
: be used as template arguments
:
: Is that true? What are the implications of this? Should I now go
around all
: my source files and make functions/global variables declared in them
static?

From the language's point of view, anonymous namespaces are a cleaner,
more general solution. As pointed out, external linkage is required
for some C++ features to be used (such as the mentioned template
instantiation). Also, 'static' linkage cannot apply to type definitions
such as struct-s or classes, and this keyword is already heavily
overloaded.
So anonymous namespaces offer a cleaner and more general mechanism,
and 'static' globals were deemed as 'deprecated'.

In practice, however, some prefer to use static, because anonymous
namespaces present some overhead (mainly in terms of long identifier
names stored in object files and in shared libraries).
In a way, they 'leak downstream' (i.e. to the linker etc) information
that the developer knows is not needed at all.
In practice, if both options are equivalent, I do not bother
converting any code. Use anonymous namespaces if they
are needed ( type definition, template... ).
In all other cases, it is a matter of choice. I will use
static if all I need is a local utility function or two.
I hope this helps,
Ivan
Feb 4 '06 #2
Asfand Yar Qazi wrote:
For years, I've been putting everything that won't be needed outside a
compilation unit in anonymous namespaces, even editing my old files that did
things the 'old' way (using static linkage). Then suddenly I find this in the
KDE developer faq:

Q: 2.24. I put some functions in anonymous namespace and someone reverted it
and made those functions static, why?
Symbols defined in a C++ anonymous namespace do NOT have internal linkage.
Anonymous namespaces only give an unique name for that translation unit and
that is it; they don't change the linkage of the symbol at all. Linkage isn't
changed on those because the second phase of two-phase name lookup ignores
functions with internal linkages. Also, entities with internal linkage cannot
be used as template arguments

Is that true? What are the implications of this? Should I now go around all
my source files and make functions/global variables declared in them static?


The FAQ is correct, but it is not answering its own question (ie, it is
not giving any reason why file static is better than anonymous
namespaces). As Ivan has pointed out, anonymous namespaces may generate
more work for the linker - on a large project like KDE this is likely
to be the reason. I'd use anonymous namespaces unless you have a good
reason not to.

Feb 4 '06 #3
Pete C wrote:


The FAQ is correct, but it is not answering its own question (ie, it is
not giving any reason why file static is better than anonymous
namespaces). As Ivan has pointed out, anonymous namespaces may generate
more work for the linker - on a large project like KDE this is likely
to be the reason. I'd use anonymous namespaces unless you have a good
reason not to.


Thank you both - I think I will look at both features from now on before
deciding which to use. I think however that I'll stick to anonymous
namespaces, as processor speeds are increasing and I'm sure they more than
compensate for a bit more compilation complexity.

--
To reply, take of all ZIGs !!
Feb 6 '06 #4

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

Similar topics

11
by: Charles Krug | last post by:
I've a function that needs to maintain an ordered sequence between calls. In C or C++, I'd declare the pointer (or collection object) static at the function scope. What's the Pythonic way to...
5
by: rossum | last post by:
Either I have found a puzzle, or I am just being stupid. Inside a namespace I have a class with a static const double member. The member is not an int so I cannot initialise it within the class,...
9
by: Bryan Parkoff | last post by:
I have noticed that C programmers put static keyword beside global variable and global functions in C source codes. I believe that it is not necessary and it is not the practice in C++. Static...
40
by: vishnu | last post by:
Hi friend, i have a problem in my program what is the use of static function in C lang? plz help me
11
by: ruffiano | last post by:
A colleague of mine who is a C developer wrote several functions in C which I now need to invoke in my C++ application. This is normally not a problem except that the header file that he wrote...
5
by: Digital Puer | last post by:
Hi, I've inherited a bunch of C code that needs to be called from a C++ framework, so I thought it would be good to put these C functions into C++ classes for better organisation. What are best...
14
by: Jess | last post by:
Hello, I learned that there are five kinds of static objects, namely 1. global objects 2. object defined in namespace scope 3. object declared static instead classes 4. objects declared...
33
by: john | last post by:
I am reading TC++PL3 and in "21.3.3 Stream State", 4 member functions returning bool are mentioned: template <class Ch, class Tr= char_traits<Ch class basic_ios: public ios_base { public: //...
3
by: David K in San Jose | last post by:
I'm using managed (CLR) C++ in VS2005 to create a Windows app that contains a form named "MyForm". In the code for that form I'm trying to invoke some static functions by using an array of function...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
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,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.