where to put using namespace std;
Asked Answered
K

7

17

I'm wondering where to put using namespace std;. I saw a code with the using namespace std; in the int main(){} but I was putting it after #include <iostream>. Where should I put it and does it make any difference where I put it?

Kelby answered 24/6, 2011 at 23:33 Comment(4)
Don't put it anywhere!Linders
@Martin: Meh, that's overly restrictive. There exist plenty of small projects/files where adding a using namespace std; isn't going to cause problems.Nunes
Absolutely never, ever put it in a header file. Best is never to use 'using namespace std' anywhere. I concur with Martin.Dromous
@Ed S: Its not about causing problems its about consistency. But a counter argument for small project use. If the project is small why do you even need using, then it becomes about being lazy.Linders
F
15

It makes a huge difference where you put it.

If you put it inside a function, then it only applies in that function.

If you put it outside a function in global scope then it applies to everything after that point.

If you put it outside a function in global scope in a header file then it will even apply to ever file that includes that header.

Generally, it's very bad practice to use it at global scope in a header, and semi bad practice to use it in global scope at all since in Unity builds, the distinction between headers and source files is blurred.

You would be best to just use it in the functions that you need it, or even not use it at all and just prefix standard library functions/classes with std::.

Favorite answered 24/6, 2011 at 23:37 Comment(0)
N
17

Adding it inside of a function restricts the scope of the using statement to that function only. You should never put a using statement inside of a header as to avoid conflicts for users of your header file(s).

Putting it above main in the file scope is fine if you know that no conflicts will arise, but even that may cause problems with other imported types and is generally to be avoided in moderately sized projects. I try to avoid pollution of the global namespace as much as possible, but if I am writing a one-off, small implementation file I will add a using namespace std; at the top for convenience.

In your case, assuming you only want to use std::cout and std::cin (just an example), you can do this:

using std::cout;
using std::cin;

Now you can write cin >> whatever and cout << whatever without fully qualifying the type/object each time and you also avoid polluting the global namespace.

Nunes answered 24/6, 2011 at 23:36 Comment(0)
F
15

It makes a huge difference where you put it.

If you put it inside a function, then it only applies in that function.

If you put it outside a function in global scope then it applies to everything after that point.

If you put it outside a function in global scope in a header file then it will even apply to ever file that includes that header.

Generally, it's very bad practice to use it at global scope in a header, and semi bad practice to use it in global scope at all since in Unity builds, the distinction between headers and source files is blurred.

You would be best to just use it in the functions that you need it, or even not use it at all and just prefix standard library functions/classes with std::.

Favorite answered 24/6, 2011 at 23:37 Comment(0)
T
8

In general (and certainaly in large projects), it's best not to use it at all.

Trophozoite answered 24/6, 2011 at 23:36 Comment(0)
S
7

The using directive is scoped, so it'll be in effect only within the scope in which it appears. Use it wherever it is most appropriate for your situation.

Stalder answered 24/6, 2011 at 23:36 Comment(0)
R
2

If you don't like typing std:: everywhere (I do, can't get enough double-colons in my code!), best practice is to selectively import the names you want into scope like so:

void some_function()
{
  using std::vector;

  vector<int> x;
  ...
}

It is ok to also use selected names at global scope.

This way you don't pull ALL of the names in the standard library into the global namespace.

Recruit answered 24/6, 2011 at 23:39 Comment(0)
T
0

It will bring everything in std into scope wherever you put it. If its at file scope, it counts everywhere in the file, if its in a function, it only applies within the function - so it depends on what you want. Personally, I don't like declaring using namespace std.

Tomika answered 24/6, 2011 at 23:38 Comment(0)
U
0

A lot of people here will say that you shouldn't be using it at all. The reason for this is there could be conflicts if you accidentally define a function that is already in the std namespace, for very large projects this could be difficult to track down. For trivial programs, it's honestly not that big of an issue, but where you put it does make a difference.

In the global scope, it applies everywhere. In a local scope (i.e, only in main()), it will only apply in main.

Here are some alternatives good alternatives

  • Put std:: before everything i.e, std::cout << "Hello, World!" << std::endl;
  • Only use the specific thing you want inside a function.

An example of using only things you want in a local function:

int main()
{
   using std::cout;
   using std::endl;
   cout << "Hello, World!" << endl;
   return 0;
} // Obviously you need #include <iostream> at the top..
Ussery answered 24/6, 2011 at 23:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.