Question about the use of namespace

I have a question about the use of namespace.

Consider the two programs below, both of which yield the same result.

What is the point of using namespace when the simpler version (version 1) achieves the same result?

Version 1:
main.cpp):
1
2
3
4
5
6
7
8
9
#include "stdafx.h"
#include <iostream>
#include "myHead.h"
using namespace std;

int main()
{
	cout << numberOfDaysInTheWeek << endl;
}


myHead.h:
1
2
3
4
5
6
#ifndef myHead_H
#define myHead_H

int numberOfDaysInTheWeek(7);

#endif 


------------------------------------

version 2:
main.cpp
1
2
3
4
5
6
7
8
9
10
#include "stdafx.h"
#include <iostream>
#include "myHead.h"
using namespace std;
using namespace myHead;

int main()
{
	cout << numberOfDaysInTheWeek << endl;
}


myHead.h:
1
2
3
4
5
6
7
8
9
#ifndef myHead_H
#define myHead_H

namespace myHead
{
	int numberOfDaysInTheWeek(7);
}

#endif  
Last edited on
The issue is that you are circumventing the entire point of namespaces with these lines:
1
2
using namespace std;
using namespace myHead;
This is considered bad practice and you shouldn't do it.
See here for why: http://stackoverflow.com/q/1452721/1959975

In your simple program, namespaces do not pose any benefit to your program. Namespaces are useful for much larger programs with many components, and are especially useful when using multiple libraries. Could you consider what would happen if two libraries you needed to use both declared a function with the same name and which accepted the same parameters? namespaces help avoid this - each library uses its own namespace.
Ah-ha!

So i should be writing out:

std::cout << "Blah blah" << std::endl;

and not:

cout << "Blah blah" << endl;

???

ALL THE TIME??!!

Isn't that a big pain in the b-hind?! Making code much more long-winded?

Sorry for the utter noobiocity of my questions.........

(edit: this quote from the link you gave makes it abundantly clear why you're right:

Keep in mind that the std namespace has tons of identifiers, many of which are very common ones (think list, sort, string, iterator etc.) which are very likely to appear in other code, too.
)
Last edited on
Generally you shouldn't be including the whole STD namespace, though it is still fine to include that which you will be using a lot.

For example let's say the file you are going to working in uses std::cout and std::cin a bunch of times in it. Instead of writing std::cout and std::cin over and over you can just include these two lines at the top of that file.

1
2
using namespace std::cout;
using namespace std::cin;


And then you can just write it like you would normally (cout << "blah";)

Though remember you should never use using namespace whateverNamespaceHere; inside of a header file. The only time it would be acceptable to use what I showed above would be inside of source files (.cpp files).

ALL THE TIME??!!

Isn't that a big pain in the b-hind?! Making code much more long-winded?


Sure you will have to write a few more character, but that isn't usually that big of a deal. It is much better to have to type a few extra characters here and there that will only take a couple of seconds to do then to have to debug naming collisions and other such problems which could take hours, days or even longer to refactor and fix.
Last edited on
I see, that's great, thanks!
Yes, it's a very good habit. You can still specifically write using std::cout; using std::endl; if you want to, but personally I find it easier to just write std:: all the time.

With longer namespace names, you can shorten them when you use them:

namespace lnn = LongNamespaceName;

I recommend only doing that in source files where you can guarantee there will not be collisions.
Last edited on
OK! I'm gonna start doing that!

Just one other question for now, i won't bother making a new thread for it:

Is it correct (according to learncpp.com it is) that one should always include the following header guard in all header files:

1
2
3
4
5
6
#ifndef FILENAME_H 
#define FILENAME_H
 
// contents of header goes here

#endif 


Yes that is correct, you should always have a header guard in your header files.

The two that I believe are in use across pretty much all compilers are

1
2
3
4
5
6
#ifndef FILENAME_H 
#define FILENAME_H
 
// contents of header goes here

#endif 


and

1
2
3
#pragma once

// contents of header here 


While it is true that #pragma once is non-standard it's functionality can be used in pretty much every single compiler out there right now if I recall correctly. Personally I prefer #pragma once over the other since it is less to type which means it is less error prone but either one works so just make sure you have one of them in your headers.
Thanks! I notice that Visual Studio automatically generates #pragma once in every new header file, so i guess i'll stick with that (although on learncpp.com he gave a reason for not using it, although i forget what the reason was)
Sorry, just back to this namespace thing for a moment:

So the actual point of namespace is to be sure you're calling up the correct thing [what's the correct word here for "thing"?] from a particular library / header file?

And not (as i thought earlier) as a way to provide an easy shortcut to writing code and to things in various libraries / header?
Last edited on
The actual point of namespaces is to prevent name collisions within large code bases and across multiple independently-developed libraries.
Clear and concisely put!
Also, don't forget the full include guards. AFAIK all modern compilers will do the same as a pragma once with the include guards, but pragma once can fail.

1
2
3
4
5
#pragma once
#ifndef MYFOO_HPP
#define MYFOO_HPP
...
#endif 

Name collisions are insidious -- without a namespace, even internal symbols can be confused at the link stage.

1
2
3
4
5
6
7
8
9
10
11
12
#pragma once
#ifndef MYFOO_HPP
#define MYFOO_HPP

#include ...

namespace joels_stuff
{
  ...
}

#endif 
to give a cross-library example, we have, between the C++ standard library, ISO library extensions, and the boost libraries,

std::copy in <algorithm>
std::expeirmental::filesystem::copy in <experimental/filesystem>
std::experimental::parallel::copy in <expeirmental/algorithm>
boost::filesystem::copy in <boost/filesystem/operations.hpp>
boost::iostreams::copy in <boost/iostreams/copy.hpp>
boost::mpl::copy in <boost/mpl/copy.hpp>
boost::range::copy in <boost/range/algorithm/copy.hpp>
boost::fusion::copy in <boost/fusion/include/copy.hpp>
boost::lambda::ll::copy in <boost/lambda/algorithm.hpp>
boost::re_detail::copy somewhere in boost/regex.. there's more, you get the idea
Topic archived. No new replies allowed.