C++ – What’s the scope of the “using” declaration in C++

c

I'm using the 'using' declaration in C++ to add std::string and std::vector to the local namespace (to save typing unnecessary 'std::'s).

using std::string;
using std::vector;

class Foo { /*...*/ };

What is the scope on this declaration? If I do this in a header, will it inject these 'using' declarations into every cpp file that includes the header?

Best Answer

There's nothing special about header files that would keep the using declaration out. It's a simple text substitution before the compilation even starts.

You can limit a using declaration to a scope:

void myFunction()
{
   using namespace std; // only applies to the function's scope
   vector<int> myVector;
}