Using Namespace

The Partridge Family were neither partridges nor a family. Discuss.
Post Reply
Walka
Posts: 19
Joined: September 7th, 2014, 3:45 am

Using Namespace

Post by Walka » August 18th, 2017, 5:57 pm

Ok so I'm lazy and I like to shorthand as much as possible while still making my code understandable, so I typically use "using namespace" but I have been reading that its bad practice to do so. Why is that?

User avatar
Manieknr1
Posts: 12
Joined: February 27th, 2017, 7:43 pm

Re: Using Namespace

Post by Manieknr1 » August 18th, 2017, 6:42 pm

If you will use namespace in header, then every file where you #include that header will also be using namespace you had set, whatever you like it or not. It can lead to some problems with varaibles and functions with the same name, and it can be pretty hard to find. On the other hand, most of the times you can use namespaces in your .cpp files cause you won't be including them anywhere so you can rest assured that the namespace will be used in that specific file only. Chilli explained this better here if i remember correctly:
https://www.youtube.com/watch?v=nR3aq6K ... matoNoodle

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Using Namespace

Post by albinopapa » August 18th, 2017, 6:49 pm

If you really want to get around having to type std:: for everything, you can make aliases.

Code: Select all

using cppstring = std::string;
template<class T>
using cppvector = std::vector<T>;
the 'using' directive/keyword in this manner creates aliases.
Usage
cppstring mystring = "Hello world!";
cppvector<int> myvec(32, 0); // creates a vector of ints with a starting count of 32 and all 32 assigned the value 0.

As Manieknr1 pointed out, the reason for namespaces is to avoid having your variable/function/data type names be the same as the ones in the other namespace.
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

Walka
Posts: 19
Joined: September 7th, 2014, 3:45 am

Re: Using Namespace

Post by Walka » August 19th, 2017, 2:15 am

So basically you shouldn't use it to keep yourself, other readers or possible the compiler from getting confused? lol

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Using Namespace

Post by chili » August 19th, 2017, 4:16 am

I don't see a huge problem with using in a cpp file, because it will be contained to that file only. But header files are meant to be included in other files, including other headers, which might be included in other headers... you get the point. If you get a name collision in a single file it's usually pretty easy to fix, but a name collision where you have to start renaming/scoping things in multiple files is gonna be a headache.

I personally do not do 'using namespace std' very often. I sometimes do like 'namespace dx = DirectX' if the namespace is annoyingly long to me, but even then only in .cpp files.
Chili

User avatar
Yumtard
Posts: 575
Joined: January 19th, 2017, 10:28 pm
Location: Idiot from northern Europe

Re: Using Namespace

Post by Yumtard » August 19th, 2017, 2:22 pm

std:: makes the code look more complicated which will make you look like more of badass hacker in the eyes of civilians. Why avoid that glory?

albinopapa
Posts: 4373
Joined: February 28th, 2013, 3:23 am
Location: Oklahoma, United States

Re: Using Namespace

Post by albinopapa » August 19th, 2017, 2:53 pm

^^Lol, right?
If you think paging some data from disk into RAM is slow, try paging it into a simian cerebrum over a pair of optical nerves. - gameprogrammingpatterns.com

User avatar
chili
Site Admin
Posts: 3948
Joined: December 31st, 2011, 4:53 pm
Location: Japan
Contact:

Re: Using Namespace

Post by chili » August 20th, 2017, 3:08 pm

My uncle Larry always told me, the more STDs the merrier.

http://aidsisnojoke.ytmnd.com/
Chili

Post Reply