What is unnamed namespace used for?

  • Thread starter Thread starter dioscuroi
  • Start date Start date
D

dioscuroi

Is there any difference between declaring in unnamed namespace and i
global namespace?

I can't recognize this


p.s: Have a nice day ^_________


-
dioscuro
 
Is there any difference between declaring in unnamed namespace and in
global namespace?

An unnamed namespace implies internal linking...
In that sense, it is commonly used as a replacement for the use of static to
hide names inside modules, but in a more powerful way.
 
dioscuroi said:
Is there any difference between declaring in unnamed namespace and in
global namespace?

I can't recognize this

To add to what Tomas said, using an unnamed namespace is the only way to
write a class that is local to the translation unit it appears in. For
example, suppose you write a couple of helper classes, each defined
differently, and each named X, which appear in a.cpp and b.cpp. If they were
to appear in the global namespace, you'd violate the one definition rule and
be rewarded with undefined behavior. This actually happened to me once in
the VC2 (or maybe VC4) days; the destructor for the X in a.cpp was called
for the X used in b.cpp. The solution to that problem is to move the classes
into anonymous namespaces, which it the one truly new capability they offer.
 
Back
Top