简体   繁体   English

是'using namespace std;' 标准的C ++函数?

[英]Is 'using namespace std;' a standard C++ function?

Is using namespace std; 正在using namespace std; a standard C++ function? 标准的C ++函数?

No. It's what's called a using directive . 不,这就是所谓的using directive I's outlined in §7.3.4 in the standard: 我在标准中的§7.3.4中概述了:

A using-directive specifies that the names in the nominated namespace can be used in the scope in which the using-directive appears after the using-directive. using-directive指定指定命名空间中的名称可以在using-directive出现在using-directive之后的范围内使用。 During unqualified name lookup (3.4.1), the names appear as if they were declared in the nearest enclosing namespace which contains both the using-directive and the nominated namespace. 在非限定名称查找(3.4.1)期间,名称看起来好像是在最近的封闭命名空间中声明的,其中包含using-directive和指定的命名空间。

Essentially, it takes everything in the namespace you specify and moves in into the namespace the directive was used. 从本质上讲,它会将您指定的命名空间中的所有内容转移到使用该指令的命名空间中。

It is not a function. 它不是一个功能。

In the following 在下面的

 using namespace std; 

the keyword using is used as a directive to introduce an entire namespace ( std in this case). 关键字using用作引入整个命名空间的指令(在本例中为std )。

Note : Always prefer using declaration over using directive 注意 :始终更喜欢using 声明而不是using 指令

No function is part of the C++ language; 没有函数是C ++语言的一部分; it's something you write or something which is already written as library and made available to you (like Standard C++ Library). 它是你写的东西或者已经写成库并且可供你使用的东西(比如标准C ++库)。 Such libraries have functions and variables put up inside segregations called namespaces . 这些库将函数和变量放在称为命名空间的隔离内。 To call a function within a namespace, you follow the syntax namespace::function_name() syntax. 要在命名空间内调用函数,请遵循语法namespace::function_name()语法。

Eg std::cout << "Hi!"; 例如std::cout << "Hi!";

While you always call a function with the function's name followed by parenthesis within with you pass arguments to the function, if any. 虽然您始终使用函数名称后跟括号内部调用函数,但您可以将参数传递给函数(如果有)。 Like function_name(args); function_name(args); . So to what you've asked: No, it isn't a function. 所以你问过的问题: 不,这不是一个功能。

using namespace std is a directive (you can think of it as a declaration to the compiler saying whatever is inside the std namespace can be used without any qualifiers from here on till the end of this compilation unit (.cpp + headers associated within) ie using namespace std是一个指令 (你可以把它看作是编译器的一个声明,说明std命名空间里面的东西可以在没有任何限定符的情况下使用,直到这个编译单元的末尾(.cpp +头部关联),即

std::cout << "Hello!"; can be used as cout << "Hello!"; 可以用作cout << "Hello!"; from there on. 从那以后。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM