Namespace implementation

Started by
7 comments, last by Drakkcon 20 years ago
Namespaces seem to be a good solution for name collision, but no-one uses them. The only namespace I''ve ever seen used is std. I''ve wanted to switch to namespaces, currently all of my functions and globals are prefixed with DS_ (diresoft), and DS:: wouldn''t be too much work. Are namespaces slow? The true general first seeks victory, then seeks battle - Sun Tzu
Advertisement
I use namespaces for ALL of my libraries ...

and there is no run-time cost, and almost as little compile time cost (in fact, well partitioned namespaces reduce the size of the global symbol table and can therefore - sometimes - increase compilation lookup speed).

Great, switching now. Thanx
and oh yeah, .NET uses them and pretty much requires them ... they even recommend how you should use them (in a .NET enviroment) ...
off the top of my head i can think of 4 librarys for C++ which use namespaces;
std as you''ve noted
boost which is a library of very usefull code
corona which is an image loading library
loki which is another code library

and my last large project used a fair few to partition classes and code into logical units (such as networking, utils etc)

so namespaces are certainly very usefull things
Namespaces just cause the compiler / linker to decorate and separate the names. They have no runtime costs, at runtime a function is still just an address of some code.

Mark
actually, they usuaully have the same runtime cost as LONG NAMES in general ... which is somtimes NONE, and sometimes simply very very little
why the hell would namespaces be slow?? Long variable names arent slow, and im sure namespaces would not affect runtime performance. I use namespaces to organize my code, and find them uber handy.

For example, all my graphics go in Reflex::Graphics --
So every time i type in Reflex::Graphics::, the intellisense gets switched on and displays all the classes/functions in the graphics package - below lies a list of the namespaces in my current project....
http://www.reflexengine.net/psamty10/doxygen/namespaces.html

Namespaces are good for much more than name collision, they were designed for code organization
also with namespaces you cna type using namespace std; at the top of a file and not have to write std:: everytime

This topic is closed to new replies.

Advertisement