2

Possible Duplicate:
Ordering of using namespace std; and includes?

I can remember that I read a tutorial that started all its source files with

using namespace std;

When I tested that with my GCC compiler it accepted the code. But is such code valid?

Community
  • 1
  • 1
Johannes Schaub - litb
  • 496,577
  • 130
  • 894
  • 1,212

4 Answers4

2

It's valid, provided you define the namespace std before:

namespace std {}
using namespace std;

It's probably not a very good idea, however, as it imposes a using namespace std; on all of the included headers.

James Kanze
  • 150,581
  • 18
  • 184
  • 329
1

Yes, it's perfectly valid.

Actually, after reading the title again . . . If you include it before any included headers, not so much. It's "valid", but not good practice.

See C++: What's happen when I use "using namespace xyz" before #include<headerxy>

Community
  • 1
  • 1
daxnitro
  • 920
  • 6
  • 7
  • What shall i do with the linked page? it doesnt provide evidence for your claim or i couldnt find it. can you please help me once again finding it? – Johannes Schaub - litb Mar 07 '12 at 09:52
  • Yeah sorry, just updated my post. – daxnitro Mar 07 '12 at 09:52
  • This answer is just wrong, this has nothing to do with good practice but with conformity to the standard. And according to the quotes provided in response to [this question](http://stackoverflow.com/q/6841130/20984), the code is not valid (even though the standard seems a bit ambiguous). – Luc Touraille Mar 07 '12 at 10:04
1

In my opinion, the only differences of using using namespace std; and not using it, is that when you use it you might run into conflicts in your code variables later (clashing of names). In addition it might contaminated someone else's namespace since when you use it you include everything in the namespace std so it's advisable not to you use it in header files. but it's up to you to use it in cpp files or not.

std::cin or cin, only difference is laziness imo. But using using namespace std; is valid.

EDIT: (For references and more clarification)

found those two references on the subject, i think you'll find the second one more helpful on why it's valid and works fine, but it's considered bad practice in some cases. Please check cplusplus and stackoverflow

Quotes:

I recommend you avoid using directives; using declarations make a lot more sense, although, personally, I would just rather use the fully qualified name.

for example, you can use:

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

instead of using the whole std.

Community
  • 1
  • 1
Reyno
  • 583
  • 13
  • 28
0

Most of the compilers follows the standard especially the fundamental specifications like this will be perfectly made par to the standard. But being an programmer I never used the declaration unless required. Most of the cases I have seen are with the school kind of programs. In the real production we've to make sense with the usage and these kind of declarations will be made close the place where we start using it. But never used before includes though it works!

sarat
  • 10,512
  • 7
  • 43
  • 74