Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » namespace { }

This thread is locked; no one can reply to it. rss feed Print
namespace { }
proftilley
Member #697
October 2000

I never saw this in school; I never saw this in a book; this "namespace" syntax.
Anybody got enough free time to explain the particulars of this? I saw it in the STL, and I just saw it again.
How's it work?

vpenquerch
Member #233
April 2000

it is used to avoid putting every global in the main name space.
say you have a library to code games, called Allegro. it defines something called rest. let's say i have another library that defines this identifier too. this means i can't use them both at the same time, as the linker will yell at me for including two things with the same name.
a solution is to hack the code.
another one is to put one of these headers (or both) in a namespace, eg:
namespace Allegro {
#include <allegro.h>
};
after that, to call Allegro's rest, you have to do:
Allegro::rest();
if a file uses Allegro a lot, it becomes a chore to prefix every identifier, so you can put the statement
using namespace Allegro;
and after that all that is contained in the allegro namespace gets poured into the global one.
you can also write
using Allegro::rest;
then you can call rest direcly, but you still have to fully qualify other identifiers from the Allegro namespace.

SystemDown
Member #663
September 2000
avatar

The namespace clause allows you to be able to place the code for certain modules under a name, for example the standard C++ classes, functions, types & templates (STL) are all under the 'std' namespace.
Namespaces mean that nothing has to be in global namespace anymore like you had to do in C, now you can encapsulate all this code under the same 'umbrella'.
It also overcomes limitations, like conflicting variable names when using several different libraries together. You simply specify the namespace when referring to the variable or type, eg. you can make a class called 'cout' if you wish, but would have to specify like blah::cout (if you called your namespace "blah"), whenever you used it anywhere. Similarly, you should always try to use std::whatever when using standard C++ features, eg std::cout, std::cin, std::endl, std::vector...
You specify a namespace like:
namespace blah { }
All your code that you wish to include in the namespace goes between the brackets, as you've probably already figured out.
You can spread this namespace across several source files, eg in "blah.h", you would put all your definitions and such under the namespace blah {} clause, and in "blah.cpp" you would put all the implementation under the same clause. You can have other sources as well using the same namespace, it simply adds the new code into the existing namespace, it does not 'overwrite' the exisiting namespace, ie. it is not akin to a redefinition of a namespace, there is no such thing.
Namespaces, I think, can also be created without supplying a name (namespace {}), but I'm not sure how this would be useful. Perhaps it is a cleaner way of implementing a global-like namespace.
Anyway I don't profess to be an expert on the matter, but I hope some of this made some sense, it's entirely too late and I must now get some sleep.
(BTW: I think I tried specifying my own namespace under DJGPP in the past, but it didn't work. Not sure about the latest version though. Under MingW32, namespaces work fine. These are the only two C++ compilers I have tried namespaces with. If you have a different compiler, namespaces may or may not work.)

---
BEER: It's not just for breakfast anymore.

SystemDown
Member #663
September 2000
avatar

Heh, beaten to the line by Vincent.
But yes, I neglected to mention the 'using' clause... silly me.
Vincent knows what he's talking about though so between the two posts you should be able to figure it out..
yawn

---
BEER: It's not just for breakfast anymore.

Mars
Member #971
February 2001
avatar

quote:in "blah.h", you would put all your definitions and such under the namespace blah {} clause
I guess you mean "declarations" and not "definitions". There is a big difference between them.

quote:I think I tried specifying my own namespace under DJGPP in the past, but it didn't work. Not sure about the latest version though
The latest DJGPP includes a newer compiler with namespace-support.

You can also say
namespace A_very_long_identifier short; .
Then
short::foo();
is the same like
A_very_long_identifier::foo(); .

If you have a namespace without a name, then it's contents do not collide with anything else -> It's a way to hide the things in the namespace.
It is impossible to acces the contents of an unnamed namespace from another source-file.
But you can use it in the same file; there is an implicit using-directive.

--
This posting is a natural product. The slight variations in spelling and grammar enhance its individual character and beauty and in no way are to be considered flaws or defects.

Gabhonga
Member #1,247
February 2001
avatar

hmm...do namespaces bring any other "benefit", like the optimizer doing a better job or something like that? I had bothered about keeping stuff in namespaces first, too (like global vars and functions that do not fit into classes), though I found in in programming practice to be just more typing-work...well, maybe the benefit comes from clearer source organization when doing teamwork, and as I'm working on my sources alone I know where everything is anyway and just don't bother about namespaces anymore...but maybe I should?

--------------------------------------------------------
sigs suck

proftilley
Member #697
October 2000

Thanks guys. This has been truly helpful.

SystemDown
Member #663
September 2000
avatar

Thanks for picking up my error Mars (the one about declarations and not definitions), in hindsight, I would've indeed said the right thing had I been more awake at the time.
I hope you can forgive me.
P.S.
"...namespace without a name, then it's contents do not collide..."
I believe you meant its and not it's, since they are grammatically different (but who really cares, right, as long as people understand what you are trying to say).

---
BEER: It's not just for breakfast anymore.

Mars
Member #971
February 2001
avatar

Whoops, you're rigth, systemdown. but at least you have understood me. Btw is there any case where one would write "it's" and not "its"?

--
This posting is a natural product. The slight variations in spelling and grammar enhance its individual character and beauty and in no way are to be considered flaws or defects.

Mars
Member #971
February 2001
avatar

To answer my question myself: to say "it is" or "it has". Doh!

--
This posting is a natural product. The slight variations in spelling and grammar enhance its individual character and beauty and in no way are to be considered flaws or defects.

vpenquerch
Member #233
April 2000

i can't see anything that an optimizer could use to generate better code, as namespaces are essentially a way of structuring scope.
or maybe if an unnamed namespace is used, then the compiler could act as if the stuff declared in it was static, and thus assume nothing will link against it in other compilation units. it could then not even emit the symbol if it can be optimized out (eg inlined for functions, etc).
but i don't know if this would be standard compliant though, and this is pure guess.
as a general case, i'd say that namespaces don't help in optimizing code.

SystemDown
Member #663
September 2000
avatar

Mars: yeah you're correct, "it's" is the short form of "it is" or "it has". But it wasn't always like that...
Up until the 19th century, "it's" was considered to be the possessive form of "it", so you would use "it's" instead of todays form of "its". They would use "'tis" as a short form of "it is" instead.
So when they decided to throw out "'tis", that's when "it's" came about to mean "it is" or "it has" and also "its" became a replacement for the old "it's". As a result, there is no case today where "it's" would be used in place of "its" since their meanings are different.

---
BEER: It's not just for breakfast anymore.

Mars
Member #971
February 2001
avatar

Languages are not logical.
"Robert's" can mean "Robert is", "Robert has", and the English genitive-replacement
"it's" is only used in a possesive way
"its" can mean "it is" or "it has"
O_o

--
This posting is a natural product. The slight variations in spelling and grammar enhance its individual character and beauty and in no way are to be considered flaws or defects.

Go to: