Stop using Namespace std! | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 21

Stop using Namespace std!

I am trying to write C++ code without "using namespace std" to get better at using C and C++ libraries. If you have C++ codes and/or resources on using C++ libraries, I would appreciate it if you share it.

16th May 2017, 2:00 AM
Manual
Manual - avatar
19 Answers
+ 16
I have some code that will demonstrate why we shouldnt use namespace std. No other resources come to mind though (sorry) as I have yet to venture outside the standard library when not using namespace std. code example: Try uncommenting using namespace std; this error occurs because there is max function in the std namespace. https://code.sololearn.com/cgXS9y00KD6C/?ref=app
16th May 2017, 2:10 AM
jay
jay - avatar
+ 7
https://code.sololearn.com/crryuPaDmrH6/?ref=app Same here. This wont work with using namespace std. So i had to manually give std:: at each place
16th May 2017, 2:12 AM
Pixie
Pixie - avatar
+ 6
@James Thank you for sharing. I have tried using a C language header with success. I used <cstdio> in my code.
16th May 2017, 5:23 AM
Manual
Manual - avatar
+ 5
@Jay thank you That is what I am going for. I have been trying to use special function like "toupper" and "tolower" from the standard C libraries in standard. I am going over the basics again on the cpp site edited http://www.cplusplus.com/doc/tutorial/program_structure It is not covered in the course on SL and needs to be used on sites like codewars and Hackerspace. Also if I can use the libraries, I can do more with C++.
16th May 2017, 2:23 AM
Manual
Manual - avatar
+ 5
@Manual: I got a 404 on that link.. odd. I was gunna have a read through as well! EDIT: lol never mind.. the last brace was in the url
16th May 2017, 2:27 AM
jay
jay - avatar
+ 5
[edited] @James You are right the course teaches people to use "namespace std" from the beginning. I found out, why using it can be a problem recently.
16th May 2017, 5:28 AM
Manual
Manual - avatar
+ 5
Hi, you gonna call me "noob" but what's wrong with namespace std?
16th May 2017, 12:58 PM
Winston-Delbert Gaster
Winston-Delbert Gaster - avatar
+ 5
@jay This article literally screwed my head... So I understood that it's easier to use another header namespace specificator to catch compiling error, right? Well, the best way to see what's the most handy is to test with his own program... So I should try to code first! 😂
24th May 2017, 4:51 PM
Winston-Delbert Gaster
Winston-Delbert Gaster - avatar
+ 5
By declaring namespaces only within the scope we are going to use them. We can limit this problem yes. If that is what you mean.
24th May 2017, 5:04 PM
jay
jay - avatar
+ 4
@Jay I got that too. I deleted the brace as well.
16th May 2017, 2:35 AM
Manual
Manual - avatar
+ 2
I made some progress, here is what I came up with. I used the C library header and std:: for output. https://code.sololearn.com/c8BaPFZ1bjhx/?ref=app
16th May 2017, 5:12 AM
Manual
Manual - avatar
+ 2
@Ace Thank you, for sharing your codes
16th May 2017, 5:16 AM
Manual
Manual - avatar
+ 2
@James That makes sense. What are resources are you using, to better your skills?
16th May 2017, 5:40 AM
Manual
Manual - avatar
16th May 2017, 1:37 PM
Manual
Manual - avatar
+ 2
Here is a good example of when using namespace std is going to mess with your code. https://code.sololearn.com/cgXS9y00KD6C/?ref=app If you uncomment using namespace std; and bring the entire standard namespace into scope you will find the program generates errors and will not compile. Why is this? Because the template function max we created conflicts with a function already defined in the standard namespace. Basically The compiler gets confused as to which max function you mean when you type max(7, 3); Do you mean the one you just made or did you mean std::max?
24th May 2017, 5:01 PM
jay
jay - avatar
+ 2
I remember back when I first started learning C++, everyone and every video at some point would say "don't use namespaces, especially the standard one". This was a practice I adopted for a short time, but didn't know why. I (very shortly) later realized this a silly practice, unless you're explicitly using methods from two different namespaces that have the same signatures. So, if you're using the "omit 'using namespace std'" method in your programs when there aren't conflicting namespaces, you're just lessening readability of your code, and wasting time by typing "std::" constantly.
26th May 2017, 9:20 PM
Nik
Nik - avatar
+ 2
@Leon lit Well, you can start with the namespace std :) instead of using cin or cout, you should use std::cin and std::cout As a beginner i will give this tip/comment to other beginners, what @Ace said is when you get at that point when you have multiple files(header and cpp) and your application/program is getting bigger you should try to make the code very clear by NOT using the namespace. So, great topic for beginners, because this is really helpful if you want to be a professional programming in the future :) I wish i had this post when I was lost in my research :))
25th Oct 2017, 9:05 PM
derXred
derXred - avatar
+ 1
I am using borland C++ and it shows the error "namespace name unexpected"
16th May 2017, 10:32 AM
Gopi Makwana
Gopi Makwana - avatar