Is it a compiler directive?
Well, in the broadest sense, every statement you write directs the compiler to do something. In a much narrower sense, in C/C++ compiler directives are generally considered to be those lines that begin with #. e.g. #include, #define, #pragma, etc.
No need to apologize for calling it a compiler directive. You got you question across effectively.
You can think of
namespace
as a way of giving a name to a scope. When you use a pair of braces, you're introducing a new scope to the compiler. namespace is simply a way of giving a name to a set of variables, classes, or functions included within the scope of that namespace. As pointed out before, namespaces are a way of preventing naming conflicts, as long as namespace names are unique.
As
TheIdeasMan mentioned, the std namespace contains commonly used names such as sort and find. If you had your own sort and find and also had
using namespace std;
the compiler would not know which one you were referring to. This is why including
using namespace std;
is not a good idea. By qualifying your reference as std::sort, the compiuler knows you want the sort function in the std namespace. You can of course, qualify the reference to your version as ::sort and the compiler will know to use the version in the global namespace. Or you can avoid the ambiguity by placing your sort function in your own namespace.
To add to
JLBorges excellent examples, there is one more example, namespace aliases. aliases are simply a shorthand way to refer to a more complicated set of namespace names.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
#include <iostream>
namespace foo {
namespace bar {
namespace baz {
int qux = 42;
}
}
}
namespace fbz = foo::bar::baz;
int main()
{
std::cout << fbz::qux << '\n';
}
|