Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Standard convention for using "std"

Tags:

c++

using

Exact Duplicate: Do you prefer explicit namespaces or ‘using’ in C++?

Which of these is a preferred convention for using any namespace?

using namespace std;

or

using std::cin;
using std::cout;

or

calling the function as and when required in the code?

std::cout<<"Hello World!"<<std::endl;
like image 941
Nikit Batale Avatar asked Dec 25 '09 17:12

Nikit Batale


People also ask

How do you declare a STD in C++?

Specify the standard namespace, for example: std::printf("example\n"); Use the C++ keyword using to import a name to the global namespace: using namespace std; printf("example\n");

How to Avoid typing std in C++?

If you really want to avoid typing std::, then you can either use something else called a using-declaration, or get over it and just type std:: (the un-solution): Use a using-declaration, which brings in specific, selected names.

What is namespace CPP?

A namespace is a declarative region that provides a scope to the identifiers (the names of types, functions, variables, etc) inside it. Namespaces are used to organize code into logical groups and to prevent name collisions that can occur especially when your code base includes multiple libraries.

How do you name a namespace in C++?

Namespaces should have unique names based on the project name, and possibly its path. Do not use using-directives (e.g., using namespace foo ). Do not use inline namespaces. For unnamed namespaces, see Internal Linkage.


1 Answers

A very good explanation is given here.

The first style i.e. using namespace whatever defeats the whole purpose of namespacing. You should never be using it except in small code snippets. (I don't use it there either! :D )

Second one is way too verbose. Not practical.

I personally like the third style i.e. typing out the fully qualified name (e.g. std::cout).

Remember, the code is read much more times than it is written & using fully qualified names IMO makes your code more readable.

like image 144
missingfaktor Avatar answered Sep 22 '22 22:09

missingfaktor