简体   繁体   中英

What is "using namespace::std" in C++

I am reading some code snippets from others and I find a line:

using namespace::std;

I suspect its purpose is using namespace std; , with some typos. But to my surprise the compiler accepts this code without any complaint. I build with:

$ g++ --version
g++ (Ubuntu 9.4.0-1ubuntu1~20.04) 9.4.0

$ /usr/bin/g++ ${SRC} -std=c++11 -pthread -Wall -Wno-deprecated -o ${OUT}

I wonder why is this code valid, and what effects will it make? I suspect it is a bad practice.

It's just same as using namespace::std; , then has the same effect with using namespace std; .

As the syntax of using-directives :

(emphasis mine)

 attr(optional) using namespace nested-name-specifier(optional) namespace-name;

nested-name-specifier - a sequence of names and scope resolution operators::, ending with a scope resolution operator. A single:: refers to the global namespace .

using namespace::std is the same as using namespace std;

The :: symbol is the scope resolution operator. When used without a scope name before it, it refers to the global namespace. This means that std is a top level namespace, and is not enclosed in another.

For example, all of the following are valid:

namespace A { namespace std{} }
using namespace::std;
using namespace A;
using namespace ::A;
using namespace::A;
using namespace A::std;

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