I'm a bit confused concerning proper usage of C++ namespaces. It is clear for me how they can help to avoid conflicts (name collision), but it is not clear anymore when it comes to the using
keyword. I mean, suppose I have a part of my code that I put into a namespace, and create a class, say
namespace my
{
class vector { ... };
}
Of course, when I use it, I wouldn't like to type my::vector
all the time, so I'd like using namespace my
. However, I could eventually need something from the std
namespace, and then I want using namespace std
at the same time, but this will bring me back to the initial name collision problem.
I know that it is possible to "import" only the functionality that I need, like using std::set
, but in this case it seems natural to import both the standard namespace std
and my
namespace completely as I'd use both of them all the time.
Does this mean that even when I use namespaces I should still think about giving non-common names to my types? Or is using namespace
a mistake and I should always type my::vector
instead? Thanks.
Well, I should probably clarify that it is more a question of readability than typing. Lots of ::
everywhere look really weird to me. I know it's a question of taste and habits, but nevertheless.
There are several techniques for avoiding name collisions, including the use of: namespaces - to qualify each name within a separate name group, so that the totally qualified names differ from each other. renaming - to change the name of one item (typically the one used less often) into some other name.
A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.
Most C code uses prefixes on functions and structs to avoid collisions, e.g sqlite3_exec() is a function belonging to SQLite3. The prefix stops the function colliding with exec() which is a standard POSIX function that got there first. So the prefix acts as a pseudo namespace.
The statement using namespace std is generally considered bad practice. The alternative to this statement is to specify the namespace to which the identifier belongs using the scope operator(::) each time we declare a type.
Of course, when I use it, I wouldn't like to type my::vector all the time, so I'd like using namespace my. However, I could eventually need something from the std namespace, and then I want using namespace std at the same time, but this will bring me back to the initial name collision problem.
Yes, it would bring you back to the initial name collision problem. This is why you should use using namespace ...;
directives sparingly, and only in source files, never in headers.
Does this mean that even when I use namespaces I should still think about giving non-common names to my types?
No, you shouldn't. Namespaces were invented precisely to avoid this.
Or is using namespace a mistake and I should always type my::vector instead?
You can, if you want to, use the using namespace ...;
or using ...;
directives until you get conflicts. This means that when you do have conflicts, you'll end up writing "unnatural" code by explicitly quallifying names in some places.
In practice, when you're dealing with short namespace names (i.e. std
), you can just type them explicitly all the time. After a week or so, you won't even notice you're typing it anymore.
If your code uses both std::vector
and my::vector
, then always writing the names in full is the best option.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With