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?
A using declaration in a definition of a class A allows you to introduce a name of a data member or member function from a base class of A into the scope of A .
using directives Use a using directive in an implementation file (i.e. *. cpp) if you are using several different identifiers in a namespace; if you are just using one or two identifiers, then consider a using declaration to only bring those identifiers into scope and not all the identifiers in the namespace.
A declarative region is a place where names can be declared in. I.e. they can be declared in a block, a class body, or in the bodies of a namespace, etc. A scope is just some snippet of program text.
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; }
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