118

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?

5
  • 31
    Just in case it's not clear from the other answers here: - Do not put a using declaration (or using directive) at file scope in an include file/header! That will cause headaches for users of the header. Commented Oct 21, 2008 at 19:07
  • 1
    In fact, do not put a using declaration (a fortiori directive) in a header at all, even within a namespace! See scope of using declaration within a namespace for problems this causes. Commented May 22, 2016 at 15:07
  • 1
    @NilsvonBarth: That's oversimplified a bit. Using using in class and function scope is safe regarding the discussed problem. Commented Oct 17, 2018 at 6:45
  • en.cppreference.com/w/cpp/language/namespace#Using-directives Commented Dec 23, 2018 at 19:24
  • You may be interested to read about the ADL C++ lookup feature. Commented Aug 5, 2019 at 4:44

8 Answers 8

136

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;
}
Sign up to request clarification or add additional context in comments.

2 Comments

I never thought I could use it inside a function!
I've got a bunch of namespaces all being used by one "conglomerator"-type file, and gmock unit-testing was extra tedious because each test used things from a specific namespace, and I thought I had to qualify each and every variable. Using using inside a function (or even a gtest TEST macro!) makes my life so much better!
71

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.

4 Comments

... which is generally a bad thing.
But if you put the using declaration inside a namespace it's limited to the scope of that namespace, so is generally OK (with the usual caveats on your particular needs and style).
...but if you do put using inside a namespace, make sure you aren't doing it to try to get around something that normally is a bad idea, like you can't encase class methods declared outside the namespace Y inside the other namespace X, just so you can locally use the namespace X. Thats why we have use namespace :: resolvers in the first place. If its that big of a problem of typing, either a macro (which can easily lead to code smells) or better yet, isolate it into it's own source .cpp where you will use the namespace there only.
While this and similar answers are good advice, they don't answer the question.
64

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.

3 Comments

I thought it wasn't possible of adding a using statement within class scope... ? I had the same question as the OP because of that as I wanted to avoid typing std:: all over the place. I got classes that uses a lot of vectors with smart pointers and the five character std:: prefix adds a lot of line length - which I find reads worse. So I was wondering if a using directive within the namespace containing the class was ok? (Even if within a header.)
What about if it's placed within the scope of a namespace { ... }?
So you can totally write : { using namespace blabla; class blah {}; } and that using will apply only to the class ?
8

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.

2 Comments

That's not strong enough. Replace avoid with Don't
BUt avoid is stronger than don't. "Avoid hitting the other cars"
6

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.

1 Comment

Note that the using declaration in a class does not behave in the same way as outside a class - eg you can't use it bring cout instead of std::cout into the scope of the class.
2

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.

Comments

1

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

4 Comments

The user of the header can then write MyNS::Foo
A better example is using boost::posix_time::ptime. Sure the user could write MyNS::ptime but that's not the end of the world, and might be offset by the convenience of being able to have functions like MyFunction(ptime a, ptime b).
Why is using std::string never ok? Even in your own namespace to save a lot of std:: prefixes?
@thomthom it is ok when wrapped in a scope such as your own namespace.
0

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"

...

5 Comments

That should only be the case when the using statement is placed before the #include: The scope of using does not extend "backwards".
@ChristophLipka So is that a bug by microsoft ? I test it with vscode and visual studio .
I can't reproduce your results in VS 2015. If I put using namespace std; before #include "header.h". I see VS recognizing string t2 as std::string. However, if I put using namespace std; after #include "header.h". I see VS reporting an error on string t2.
Test again , I think that is a bug from intellisense because it can't compile.
As an additional note: It is generally expected that header files work "as-is", without requiring any additional statements in the including file. Therefore, writing a header file in such a way that it requires a using statement in the including file is usually discouraged.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.