Namespaces good for anything else?

Started by
19 comments, last by Mussi 11 years, 4 months ago
Hi.

So I been wondering if namespaces is good for anything else but preventing name collisions?
For example: Using a namespace to make it clearer what belongs to a game engine.

If you can tell me any other uses, please do.

Thanks
Advertisement
As far as I know, not really, but naming collisions can be a bigger deal than you might realize. For example, if you have a private free function in your engine named [font=courier new,courier,monospace]loadFile()[/font], and a user of your engine also makes their own private free function named [font=courier new,courier,monospace]loadFile()[/font], and their symbols would collide in the linking stage (assuming their signatures matched), even though the user never included any header from your engine that said anything about [font=courier new,courier,monospace]loadFile()[/font]. Using namespaces would be one way to prevent these symbols from colliding while still allowing multiple translation units (source files) to use the functions, which you should do so that end users can be happy.
[size=2][ I was ninja'd 71 times before I stopped counting a long time ago ] [ f.k.a. MikeTacular ] [ My Blog ] [ SWFer: Gaplessly looped MP3s in your Flash games ]
I use a namespace called 'internal' to hide stuff that needs to be in a public header, but shouldn't be directly used by the API user.

I always put enums in a namespace, and then call the enum itself 'Type' so that their usage looks like:
MyOptions::Type var = MyOptions::Choice1;

If you have some functions/variables that need to be private to a single CPP file (I.e. can't be accessed with extern, etc) you can put them in an anonymous namespace.
Namespaces can be useful lines of demarcation between subsystems. We use that all the time on our games.

They can also be useful to put standalone functions in a hierarchy, especially those functions that don't really belong to a class. It is a much better approach than some languages that require static functions in a utility class.

I always put enums in a namespace, and then call the enum itself 'Type' so that their usage looks like:
MyOptions::Type var = MyOptions::Choice1;


Don't we now have strongly typed enum?
An invisible text.
Don't we now have strongly typed enum?
Yes, C++11's "[font=courier new,courier,monospace]enum class[/font]" is a big improvement that fixes several issues with C++'s "[font=courier new,courier,monospace]enum[/font]". The "[font=courier new,courier,monospace]namespace[/font]/[font=courier new,courier,monospace]struct[/font] wrapper around [font=courier new,courier,monospace]enum[/font]" pattern isn't necessary if you use [font=courier new,courier,monospace]enum class[/font].

However, I'm still supporting C++03 compilers, so I'd rather not use C++11 code where it's not necessary.

If you have some functions/variables that need to be private to a single CPP file (I.e. can't be accessed with extern, etc) you can put them in an anonymous namespace.

I like the idea of anonymous namespaces but I dislike the way they interact with most tools, often including the IDEs I use. Basically, anonymous namespaces are usually implemented by creating a namespace with a pseudo-randomized name, so they make the actual symbol names really long and it just gets annoying dealing with the outputs of things like stack traces and profiler runs. Especially since the long randomized name is specific to a translation unit, and the profilers and debuggers already display file names for symbols. In theory it's useful if you do something like stick an anonymous namespace in a header, but since none of the symbol demanglers I've run across will get you to the translation unit from the anonymous namespace tag, it's pretty pointless (well maybe they do, but I've given up on anonymous namespaces long enough ago that I don't know it's currently a feature).
Do you remember which compilers those were? I just tried it in MSVC 2008 and the stack trace shows
> createproxy-d.exe!`anonymous namespace'::locateGDALDataInternal() Line 61 C++
Unfortunately I do not have other MSVC versions or Clang/gcc around to test this more thoroughly.

Namespaces can be useful lines of demarcation between subsystems. We use that all the time on our games.

They can also be useful to put standalone functions in a hierarchy, especially those functions that don't really belong to a class. It is a much better approach than some languages that require static functions in a utility class.

But those languages often don't support standalone functions anyway and thus you have to use the static method function clutch because of that. In languages that do support standalone functions the file they are in is often the module that achieves the same semantics as namespaces in C++.

Worked on titles: CMR:DiRT2, DiRT 3, DiRT: Showdown, GRID 2, theHunter, theHunter: Primal, Mad Max, Watch Dogs: Legion

Beside avoiding name conflicting, I often use namespace for,

1, Replace "static" keyword with unnamed namespace in source file.
2, Hide private/internal symbol in the header file.
namespace _internal { class MyInternal {}; }
_internal is still visible to others, but with the name, the others know it's for internal usage.

https://www.kbasm.com -- My personal website

https://github.com/wqking/eventpp  eventpp -- C++ library for event dispatcher and callback list

https://github.com/cpgf/cpgf  cpgf library -- free C++ open source library for reflection, serialization, script binding, callbacks, and meta data for OpenGL Box2D, SFML and Irrlicht.

This topic is closed to new replies.

Advertisement