Using Namespace std

2020-03-30 01:29发布

I am taking a programming class in school and I wanted to start doing some c++ programming out of class. My school using Microsoft Visual C++ 6.0 (which is from 1998) so it still uses <iostream.h> rather than <iostream> and using namespace std. When I started working, I couldn't figure out how and when to use using namespace std and when to just use things like std::cout<<"Hello World!"<<'\n'; (for example) as well as it's limits and other uses for the namespace keyword. In particular, if I want to make a program with iostream and iomanip, do I have to state "using namespace std" twice, or is there something different that I would have to use as well, or can I just do the same thing as I did with iostream? I tried googling it but I didn't really understand anything. Thanks in advance for the help.

5条回答
对你真心纯属浪费
2楼-- · 2020-03-30 01:42

If you use the header names without the .h, then the stuff declared/defined in it will be in the std namespace. You only have to use using namespace std; once in the scope where you want stuff imported in order to get everything; more than one using namespace std; doesn't help anything.

I'd recommend against using namespace std; in general, though. I prefer to say, for example, using std::cout; instead, in order to keep names in std from conflicting with mine.

For example:

#include <iostream>
#include <iomanip>    

int main()
{
     using namespace std;

     int left = 1, right = 2;
     cout << left << " to " << right << "\n";
}

may cause mysterious issues, because left and right exist in the std namespace (as IO manipulators), and they get imported if you lazily say using namespace std;. If you meant to actually use the IO manipulators rather than output the variables, you may be a bit disappointed. But the intent isn't obvious either way. Maybe you just forgot you have ints named left and right.

Instead, if you say

#include <iostream>
#include <iomanip>

int main()
{
    using std::cout;

    int left = 1, right = 2;
    cout << left << " to " << right << "\n";
}

or

#include <iostream>
#include <iomanip>

int main()
{
     int left = 1, right = 2;
     std::cout << left << " to " << right << "\n";
}

everything works as expected. Plus, you get to see what you're actually using (which, in this case, includes nothing from <iomanip>), so it's easier to keep your includes trimmed down to just what you need.

查看更多
兄弟一词,经得起流年.
3楼-- · 2020-03-30 01:55

Here is a good link that describes namespaces and how they work.

Both methods are correct, that is, you can either introduce a namespace with the "using" statement or you can qualify all the members of the namespace. Its a matter of coding style. I prefer qualifying with namespaces because it makes it clear to the reader in which namespace the function / class is defined.

Also, you do not have to introduce a namespace twice if you are including multiple files. One using statement is enough.

查看更多
放荡不羁爱自由
4楼-- · 2020-03-30 01:55

Specific to using namespace std

You really shouldn't ever use it in a header file. By doing so, you've imported the entire 'std' into the global namespace for anyone who includes your header file, or for anyone else that includes a file that includes your file.

Using it inside a .cpp file, that's personal preference. I typically do not import the entire std into the global namespace, but there doesn't appear to be any harm in doing it yourself, to save a bit of typing.

查看更多
仙女界的扛把子
5楼-- · 2020-03-30 02:08

Ok, handful of things there, but it is manageable.

First off, the difference between:

using namespace std;
...
cout << "Something" << endl;

And using

std::cout << "Something" << std::endl;

Is simply a matter of scope. Scope is just a fancy way of saying how the compiler recognizes names of variables and functions, among other things. A namespace does nothing more than add an extra layer of scope onto all variables within that namespace. When you type using namespace std, you are taking everything inside of the namespace std and moving it to the global scope, so that you can use the shorter cout instead of the more fully-qualified std::cout.

One thing to understand about namespaces is that they stretch across files. Both <iostream> and <iomanip> use the namespace std. Therefore, if you include both, then the declaration of using namespace std will operate on both files, and all symbols in both files will be moved to the global scope of your program (or a function's scope, if you used it inside a function).

There are going to be people who tell you "don't use using namespace std!!!!", but they rarely tell you why. Lets say that I have the following program, where all I am trying to do is define two integers and print them out:

#include <iostream>

using namespace std;

int main(int argc, char** argv) {
    int cout = 0;
    int endl = 1;
    cout << cout << endl << endl;     // The compiler WILL freak out at this :)
    return 0;
}

When I use using namespace std, I am opening the door for naming collisions. If I (by random chance), have named a variable to be the same thing as what was defined in a header, then your program will break, and you will have a tough time figuring out why.

I can write the same program as before (but get it to work) by not using the statement using namespace std:

#include <iostream>

int main(int argc, char** argv) {
    int cout = 0;
    int endl = 1;
    std::cout << cout << endl << std::endl; // Compiler is happy, so I'm happy :)
    return 0;
}

Hopefully this has clarified a few things.

查看更多
小情绪 Triste *
6楼-- · 2020-03-30 02:08

Good question, Ryan. What using namespace does is importing all symbols from a given namespace (scope) into the scope where it was used. For example, you can do the following:

namespace A {
  struct foo {};
}

namespace B {
  using namespace A;

  struct bar : foo {};
}

In the above examples, all symbols in namespace A become visible in namespace B, like they were declared there.

This import has affect only for a given translation unit. So, for example, when in your implementation file (i.e. .cpp) you do using namespace std;, you basically import all symbols from std namespace into a global scope.

You can also import certain symbols rather than everything, for example:

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

You can do that in global scope, namespace scope or function scope, like this:

int main ()
{
   using namespace std;
}

It is up to a programmer to decide when to use fully qualified names and when to use using keyword. Usually, it is a very bad taste to put using into a header files. Professional C++ programmers almost never do that, unless that is necessary to work around some issue or they are 100% sure it will not mess up type resolution for whoever use that header.

Inside the source file, however (nobody includes source files), it is OK to do any sort of using statements as long as there are no conflicting names in different namespaces. It is only a matter of taste. For example, if there are tons of symbols from different namespaces being used all over the code, I'd prefer at least some hints as for where they are actully declared. But everyone is familiar with STL, so using namespace std; should never do any harm.

There also could be some long namespaces, and namespace aliasing comes handy in those cases. For example, there is a Boost.Filesystem library that puts all of its symbols in boost::filesystem namespace. Using that namespace would be too much, so people usually do something like this:

namespace fs = boost::filesystem;

fs::foo ();
fs::bar ();

Also, it is almost OK to use namespace aliasing in headers, like this:

namespace MyLib {
  namespace fs = boost::filesystem;
}

.. and benefit from less typing. What happens is that users that will use this header, will not import the whole filesystem library by saying using namespace MyLib;. But then, they will import "fs" namespace from your library that could conflict with something else. So it is better not to do it, but if you want it too badly, it is better than saying using namespace boost::filesystem there.

So getting back to your question. If you write a library using C++ I/O streams, it is better not to have any using statements in headers, and I'd go with using namespace std; in every cpp file. For example:

somefile.hpp:

namespace mylib {

class myfile : public std::fstream {
public: 
  myfile (const char *path);

 // ...
};

}

somefile.cpp:

#include "somefile.hpp"

using namespace std;
using namespace mylib;

myfile::myfile (const char *path) : fstream (path)
{
  // ...
}
查看更多
登录 后发表回答