What are the advantages of:
1) namespaces
2) including headers in a namespace
3) reverse of " using " namespace

- 2,778
- 3
- 32
- 60

- 1,546
- 3
- 14
- 28
-
https://stackoverflow.com/questions/41590/how-do-you-properly-use-namespaces-in-c – Cory Kramer Aug 23 '14 at 18:20
-
Do your own homework. – The Paramagnetic Croissant Aug 23 '14 at 18:44
1 Answers
1) Namespaces avoid naming conflicts in complex projects.
For example the boost library and the standard library both have a class regex
and a class thread
. Without namespace, you couldn't use both libraries together. With namespace you can specify the name, for example: boost::regex
and std::thread
.
Another example, is when you have several versions of code that coexist. This article shows a nice example for libraries, using nested namespaces and appropriate using
statemens.
2) Namespace accross source files
Declaration/definition need not to be contiguous. You could have several blocks of namespace in source files, refering to the same namespace. So no problem of using in headers or in bodies.
Example: File source.h
namespace cth {
void doSomethingGreat(); // forward declaration
}
File source.cpp
#include "Source.h"
int main() // not in the cth namespace !
{
cth::doSomethingGreat(); // use what has been declared in the header
}
namespace cth {
void doSomethingGreat( ) { // define the function of the header
// ... something great here
}
}
If you forget the namespace for the definition of the function for example, you will have linking errors.
Now you could include the header after the opening of a namespace. It's valid, but remember that your compiler will include the file and handle it as if its content would be written where you included it. And this might give surprising results.
For example if I change slightly the code in source.cpp so that it starts like this:
namespace cth {
#include "Source.h"
// ... something other stuff here
}
Well it would work, but the definition in the header will be understood as a nested namespace. Thus the file will not compile, because the function cth::doSomethingGreat()
has no forward declaration before main()
. But the header would define cth::cth::doSomethingGreat()
.
If you follow the principle that a header file is something that should be used publicly, you should make it self contained in terms of namespace.
3) The using namespace facilitates the work.
In the first example, you could have:
using boost::regex;
using std::thread;
If later you change your mind, you could change the using in your source code to switch to the other class (if they use the same API, otherwhise, more work would be required !).
You can define a using
in any block of code, and it applies until the end of the block.
You can add several using in the same block for the same name, but it's illegal if it creates an ambiguity.
using cth::doSomethingGreat;
doSomethingGreat(); // will use cth::doSomethingGreat
using oth::doSomethingGreat; // allowed
doSomethingGreat(); // ERROR if same signature: compiler doesn't know if he should use the cth:: or oth:: function.
But you can have different using n different blocs:
{
using cth::doSomethingGreat;
doSomethingGreat(); // use what has been declared in the header
}
{
using oth::doSomethingGreat;
doSomethingGreat(); // use what has been declared in the header
}

- 68,716
- 7
- 72
- 138