Reputation: 1369
This may be a duplicate but I didn't see exactly similar question.
Does a using namespace
declaration stops at the end of it's source file if not written in header ?
foo.cpp
using namespace foo; // will this using namespace's scope end at the end of this file?
some random code...
or do we need to create a scope ?
bar.cpp
{
using namespace bar;
some random code...
}
Upvotes: 2
Views: 85
Reputation: 4439
The short answer is that in the 99% of time your using namespace ...
is limited to the single .cpp it's in and other .cpp files don't have it.
But you should learn about compilation units and how they work with the preprocessor. Usually a single compilation unit is a single source file. That single source file might often have #include
instructions to the preprocessor. If your using namespace bar;
happens before that #include
, then the namespace will pollute that header.
You won't see that pollution from another .cpp file unless the namespace causes differences. So for example, if you have a namespaced function bar::min
, then using namespace bar;
right before #include
will change what min
functions are available to the header. So in foo.cpp
it might use bar::min
but then in bar.cpp
it might use std::min
. That might or might not cause errors, warnings, or even differences at runtime. It really depends on what names are affected.
Upvotes: 4