Namespaces good for anything else?

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

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.


You should consider using another name for this. Global names beginning with an underscore are reserved for the compiler. This might cause you some problems someday...
Advertisement

You should consider using another name for this. Global names beginning with an underscore are reserved for the compiler. This might cause you some problems someday...

yeah, that is for example.
my real code uses sub system name as prefix, such as foo_internal.
also, i think single underscore is safe, double underscore is reserved for comipler?

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.

According to this Stack Overflow post, _internal should be safe unless it is in the global namespace.
It could also mean that there is a global name _internal which might produce some ambiguities. But foo_internal is a completely different story and should be perfectly safe.
Well, the wording in the standard appears to be explicitly "Each name that begins with an underscore is reserved to the implementation for use as a name in the global namespace." which is quite distinct from the order of words chosen for anything with a double underscore or underscore plus uppercase letter which are "reserved to the implementation for any use". The standard went quite a bit out of the way to explain where it is forbidden (the global namespace only).
So a standard compliant implementation cannot really use anything that would cause ambiguities with something like
namespace myname {
namespace _internal {
// ...
}
}

Anything not respecting the scoping rules here would have to be named with a double underscore or underscore plus uppercase letter.

While I would personally avoid _internal as a personal preference, remember there is a whole coding convention built around _name for member variables and name_ for parameters (I don't like it either, but sporadically you encounter it). Without the global namespace only rule, that would lead to quite possible name clashes.

For example: Using a namespace to make it clearer what belongs to a game engine.

Yes, namespaces are definitely useful for telling you which subsystem or library a class belongs in.

For example, I have Common:: (non-specific code - my general code collection), Engine:: (genre-specific code), Game:: (game-specific code).
I also have Engine::World:: and Common::Input:: and other such embedded namespaces to contain classes or functions that go together in a collection.
1. You can easily add anything into any namespace except std without looking into that namespace
2. Not quite sure about this one but I think it is worthy to mention tho. Looking up machanism, if your caller's parameter is in a namespace the compiler will first try to find the most matched function in that namespace then globle space.
I don't really like seeing namespaces to break down subsystems in C++ projects. Similarly, I don't think there is a need for nested namespaces.

That said, I use a single top level namespace for each project I work on and heavy use of anonymous namespaces for implementation hiding.
Just for the record, C++ namespaces can also be used to perform compile-time selection of alternate implementations. You don't see it very often, but when it's needed it's very useful.

Stephen M. Webb
Professional Free Software Developer


I don't really like seeing namespaces to break down subsystems in C++ projects. Similarly, I don't think there is a need for nested namespaces.
Nested namespaces might make sense when you are writing a library and want to prevent accidental argument-dependent lookups? You could have your public datatypes and anything you intend to offer via ADL in the main namespace, and anything you want to be explicitly accessible but hidden from ADL would go in a nested namespace.

This topic is closed to new replies.

Advertisement