C++ namespace and include

2019-01-30 19:00发布

Why do we need both using namespace and include directives in C++ programs ?

For example,

#include <iostream>

using namespace std;

int main() {
 cout << "Hello world";
}

Why is it not enough to just have #include or just have "using namespace std" and get rid of the other ?

(I am think of an analogy with Java, import java.net.* will import import everything from java.net, you don't need to do anything else.)

11条回答
ゆ 、 Hurt°
2楼-- · 2019-01-30 19:43

In C++, the include directive will copy and paste the header file into your source code in the preprocessing step. It should be noted that a header file generally contains functions and classes declared within a namespace. For example, the <vector> header might look similar to something like this:

namespace std {
    template <class T, class Allocator = allocator<T> > class vector;
    ...
} 

Supposing you need to define a vector in your main function, you do #include <vector> and you have the piece of code above in your code now:

namespace std {
    template <class T, class Allocator = allocator<T> > class vector;
    ...
}
int main(){
   /*you want to use vector here*/
}

Notice that in your code the vector class is still located in the std namespace. However, your main function is in the default global namespace, so simply including the header will not make the vector class visible in global namespace. You have to either use using or do prefixing like std::vector.

查看更多
一夜七次
3楼-- · 2019-01-30 19:45

The include is defining the existence of the functions.

The using is making it easier to use them.

cout as defined in iostream is actually named "std::cout".

You could avoid using the namespace by writing.

std::cout << "Hello World";
查看更多
欢心
4楼-- · 2019-01-30 19:45

I think the other answers are missing the point slightly. In all of C++, Java and C#, the using/import thing is entirely optional. So that's not different.

And then you have to do something else to make code be visible anyway, in all three platforms.

In C++, you minimally have to include it into the current translation unit (good enough for many implementations of vector, string, etc.), often you have to add something to your linker as well, although some libraries do that automatically based on the include (e.g. boost when building on Windows).

And in C# you have to add a reference to the other assembly. That takes care of the equivalent of includes and link settings.

And in Java you have to ensure the code is on the classpath, e.g. adding the relevant jar to it.

So there are very closely analogous things required on all three platforms, and the separation between using/import (a convenience) and actual linkage resolution (a requirement) is the same in all three.

查看更多
做自己的国王
5楼-- · 2019-01-30 19:46

You need to understand namespaces if you want to truly understand this.

With include you are just including the header file.

With using namespace you are declaring you are using a given namespace that contains stuff such as cout. so if you do this:

using namespace std;

to you use cout you can just do

cout << "Namespaces are good Fun";

instead of:

std::cout << "Namespaces are Awesome";

Note that if you do not #include <iostream> you won't be able to use neither std::cout nor cout in your declarations and so forth because you're not including the header.

查看更多
孤傲高冷的网名
6楼-- · 2019-01-30 19:50

One liner (not that this is something new :)):

using std allows you to omit std:: prefix, but you cannot use cout at all without iostream.

查看更多
虎瘦雄心在
7楼-- · 2019-01-30 19:52

These keywords are used for different purposes.

The using keyword makes a name from a namespace available for use in the current declarative region. Its mostly for convenience so that you do not have to use the fully qualified name all the time. This page explains it in some detail.

The #include statement is a pre processor directive and it tells the preprocessor to treat the contents of a specified file as if those contents had appeared in the source program at the point where the directive appears. That is, you can think of this statement as copying the included file into the current one. The compiler then compiles the entire file as if you wrote all the code in one big file.

查看更多
登录 后发表回答