+ 1

C++ advises

When coding in C++, for best practice, is it recommended to keep the line "using namespace std;" or not? #include <iostream> using namespace std; int main() { cout <<"Hello World!\n"; return 0; } or like this #include <iostream> int main() { std::cout <<"Hello World!\n"; return 0; } or like this #include <iostream> using std::cout; int main() { cout <<"Hello World!\n"; return 0; } PS: I am not asking which one will work, I am asking which one is the best way to practice on the long run

23rd Sep 2019, 4:36 PM
Joseph Ahouandjinou
Joseph Ahouandjinou - avatar
17 Respostas
+ 5
[Part-1] A namespace is basically a collection of name definitions. It is used to prevent the use of incorrect names for variables or classes. Suppose you and your friend are working on the same project but on different parts of it. It is possible that both of you use the same name (say, 'prog') for different classes or variables at the same scope level. Both of you use the name 'prog' assuming that you were using it for the thing defined by you. This may lead to infuriating errors and ruin your project. Namespaces define different implementations for the same name under their scope. So "using namespace std" asks the compiler to refer to the 'std' (standard) namespace when working out the cin and cout (the job that we know they do) instead of any other implementations in the iostream directive. For codes with shorter parts of cin and cout, it is strongly recommended to use "std::cin" and "std::cout" as they minimise the chances of scope-related errors....
25th Sep 2019, 2:31 PM
SSki11
SSki11 - avatar
+ 4
[Part-2] ...if you are a beginner or quite confident that you won't mess with the scope, you can use "using namespace std;" globally. If you want to use cin or cout with their standard usage frequently in the same function or basically the same scope, and you wish to use a different application of cin and cout in the rest of the code, then instead of including the above line globally just add it at the beginning of the definition of the desired function or class method, etc. e.g. {GLOBAL} using namespace std; void function1(){ cin >> something; // standard use cout << something; // standard use } void function2(){ cin >> something; // standard use cout << something; // standard use } e.g. {NON-GLOBAL} void function1(){ using namespace std; cin >> sth; // standard use cout << sth; // standard use } cout << "Hello"; // shows error std::cout << "Hello"; // standard use
25th Sep 2019, 2:46 PM
SSki11
SSki11 - avatar
+ 3
it's heavily recommend to use namespace as it removes any duplicable issues P.S. you can still use cout and cin by using std::cout & std::cin
25th Sep 2019, 2:00 AM
Aditya
Aditya - avatar
+ 1
just keep in mind that if you create a function with the same name as one in your included library, you will get a compile error
23rd Sep 2019, 4:43 PM
Bebida Roja
Bebida Roja - avatar
+ 1
both are right, its just a question of taste. by compile error i mean the program will not run.
23rd Sep 2019, 4:56 PM
Bebida Roja
Bebida Roja - avatar
+ 1
what im saying is that if you define, as an example, cout, you will get an error because it is already defined in std namespace. you could overload them, but that is the warning, that you may define some function that is already defined in std,
23rd Sep 2019, 5:33 PM
Bebida Roja
Bebida Roja - avatar
+ 1
if you think your defined functions dont redefine already defined functions, its fine, thats the only warning/error you will get
23rd Sep 2019, 6:21 PM
Bebida Roja
Bebida Roja - avatar
+ 1
Using namespace std makes it easier to write but industry standards prefer you dont because it calls other libraries and therefore isn't efficient. So if you're thinking of a serious C++ position I would get used to using std::
24th Sep 2019, 6:31 AM
kc101010
+ 1
Thanks BlueBoiKc
24th Sep 2019, 6:37 AM
Joseph Ahouandjinou
Joseph Ahouandjinou - avatar
+ 1
Hello
24th Sep 2019, 4:15 PM
Bagtyyar Batyrow
Bagtyyar Batyrow - avatar
+ 1
i like using the first method it's the most common but if you want to get into namespace then start using std::
24th Sep 2019, 5:35 PM
Pickle_Rick()
Pickle_Rick() - avatar
0
Now I see what you mean Bebida Roja , but I don't think it answers my question, but thank you
23rd Sep 2019, 5:52 PM
Joseph Ahouandjinou
Joseph Ahouandjinou - avatar
0
Bebida Roja Nope, that's not what I'm saying and again my main question was the most recommended way to practice coding in c++: #include <iostream> using namespace std; int main() { cout <<"\n"; return 0; } Or: #include <iostream> int main() { std::cout <<"\n"; return 0; } Or: #include <iostream> using std::cout; int main() { cout <<"\n"; return 0; } And again I know all the examples will work without compiling an error( Hence I don't understand why the compile error explanation)
23rd Sep 2019, 6:26 PM
Joseph Ahouandjinou
Joseph Ahouandjinou - avatar
0
Also I modified my question so that anyone else who wants to reply knows exactly what I'm asking
23rd Sep 2019, 6:29 PM
Joseph Ahouandjinou
Joseph Ahouandjinou - avatar
0
Thank you Shadow
23rd Sep 2019, 7:40 PM
Joseph Ahouandjinou
Joseph Ahouandjinou - avatar
24th Sep 2019, 4:17 PM
Joseph Ahouandjinou
Joseph Ahouandjinou - avatar