What is the scope of "use" in C ++?

I use the 'using' declaration in C ++ to add std :: string and std :: vector to the local namespace (to save the input of unnecessary "std ::").

using std::string; using std::vector; class Foo { /*...*/ }; 

What is the scope of this declaration? If I do this in the header, will it inject these โ€œuseโ€ declarations into every cpp file that includes the header?

+83
c ++
Oct. 21 '08 at 18:52
source share
7 answers

When you # include the header file in C ++, it puts the entire contents of the header file in the place you included in the source file. Thus, including the using declaration file has the exact effect of placing the using declaration at the top of each file that includes this header file.

+52
Oct 21 '08 at 18:56
source share

There is nothing special about the header files that save the using declaration. This is a simple substitution of text before compilation.

You can restrict the declaration using scope:

 void myFunction() { using namespace std; // only applies to the function scope vector<int> myVector; } 
+96
Oct 21 '08 at 18:57
source share

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

  • Located at the top of the file, it has scope in this file.
  • If it is a header file, it will have scope in all files that contain this header. In general, this is a "not a good idea", as it can have unexpected side effects.
  • Otherwise, the using statement has a scope within the block that contains it from the point at which it occurs to the end of the block. If placed inside a method, it will have a scope inside that method. If placed in a class definition, it will have scope within that class.
+45
Oct 21 '08 at 19:53
source share

In the above case, a file (โ€œtranslation unitโ€), which means โ€œyes,โ€ each file that includes it.

You can also put the using statement inside the class, in which case it is valid only for this class.

Typically, if you need to specify a namespace in the header, it is often best to fully qualify each required identifier.

+6
Oct 21 '08 at 18:53
source share

A scope is a scope in which a usage declaration is used.

If this is a global area, then it will be located on a global scale. If it is in the global area of โ€‹โ€‹the header, then it will be in the global area of โ€‹โ€‹each source file that includes the header.

So, a general tip: avoid using ads in the global heading area .

+5
Oct 21 '08 at 19:46
source share

It is right. A scope is a module that uses a using declaration. If any header files contained in a module contain using declarations, the scope of these declarations will be this module, as well as any other modules that contain the same headers.

+2
Oct 21 '08 at 18:54
source share

There are a few comments that are quite unskilled when they say "Do Not." This is too strict, but you must understand when everything is in order.

A record using std::string will never be OK. Writing using ImplementationDetail::Foo in your own header when this header declares that the implementation ofDetail :: Foo may be OK, moreso if a usage declaration occurs in your namespace. For example.

 namespace MyNS { namespace ImplementationDetail { int Foo; } using ImplementationDetail::Foo; } 
+1
Oct 22 '08 at 8:42
source share



All Articles