What's the scope of the "using" declaration in C++?

C++

C++ Problem Overview


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?

C++ Solutions


Solution 1 - C++

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;
}

Solution 2 - C++

When you #include a header file in C++, it places the whole contents of the header file into the spot that you included it in the source file. So including a file that has a using declaration has the exact same effect of placing the using declaration at the top of each file that includes that header file.

Solution 3 - C++

The scope of the using statement depends on where it is located in the code:

  • Placed at the top of a file, it has scope throughout that file.
  • If this is a header file, it will have scope in all files that include that header. In general, this is "not a good idea" as it can have unexpected side effects
  • Otherwise the using statement has scope within the block that contains it from the point it occurs to the end of the block. If it is placed within a method, it will have scope within that method. If it is placed within a class definition it will have scope within that class.

Solution 4 - C++

The scope is whatever scope the using declaration is in.

If this is global scope, then it will be at global scope. If it is in global scope of a header file, then it will be in the global scope of every source file that includes the header.

So, the general advice is to avoid using declarations in global scope of header files.

Solution 5 - C++

In the case cited, the file ("translation unit"), which means yes, every file that includes it.

You can also put the using statement inside the class, in which case, it's in effect just for that class.

Generally, if you need to specify a namespace in a header, it's often best to just fully-qualify every identifier necessary.

Solution 6 - C++

That is correct. The scope is the module that uses the using declaration. If any header files that a module includes have using declarations, the scope of those declarations will be that module, as well as any other modules that include the same headers.

Solution 7 - C++

There are a few comments that are rather unqualified when they say "Don't". That is too stern, but you have to understand when it is OK.

Writing using std::string is never OK. Writing using ImplementationDetail::Foo in your own header, when that header declares ImplementationDetail::Foo can be OK, moreso if the using declaration happens in your namespace. E.g.

namespace MyNS {
    namespace ImplementationDetail {
        int Foo;
    }
    using ImplementationDetail::Foo;
}

Solution 8 - C++

Edited:

As a additional information , put "using" in your source file will affect your header when the statement is placed "before the #include" . ( The scope of using does not extend "backwards" )

header.h

//header.h
#include <string>

std::string t1; // ok
string t2; // ok

header.cpp

//header.cpp
using namespace std ;

#include "header.h"

...

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJeff LakeView Question on Stackoverflow
Solution 1 - C++EclipseView Answer on Stackoverflow
Solution 2 - C++Paige RutenView Answer on Stackoverflow
Solution 3 - C++dagorymView Answer on Stackoverflow
Solution 4 - C++JohnMcGView Answer on Stackoverflow
Solution 5 - C++James CurranView Answer on Stackoverflow
Solution 6 - C++Ates GoralView Answer on Stackoverflow
Solution 7 - C++MSaltersView Answer on Stackoverflow
Solution 8 - C++StardussttView Answer on Stackoverflow