简体   繁体   中英

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

Is using namespace std; a standard C++ function?

No. It's what's called a using directive . I's outlined in §7.3.4 in the standard:

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. 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.

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).

Note : Always prefer using declaration over using directive

No function is part of the C++ language; it's something you write or something which is already written as library and made available to you (like Standard C++ Library). 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.

Eg 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); . 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

std::cout << "Hello!"; can be used as cout << "Hello!"; from there on.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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