4

I looked trough some of my older code and found that I was using the using namespace directive. From what I read in a lot of google results, it seems that it is never a good idea to use this. Is there actually a valid use case for this construct or was this just a misguided attempt to make peoples lifes easier, that failed?

Deduplicator
9,2395 gold badges33 silver badges53 bronze badges
asked Jan 20, 2018 at 14:32
1
  • 1
    Maybe your google results are just misguided, or you misinterpreted them, hard to tell since you "forgot" to give any reference. Commented Jan 20, 2018 at 19:07

2 Answers 2

3

Yes, certainly there are valid uses for the using namespace-directive.

For example, if you want to use user-defined literals, there operators must be in-scope, so:

using namespace std::literals;

Or maybe only

using namespace std::string_literals;

Generally, using namespace is unconscionable when you don't know the exact contents, now and in the future, of the respective namespace. If you do, you are fine. But the poster-child of using namepace-abuse, namespace std, is far too big, amorphous and changing for considering it.

answered Jan 20, 2018 at 14:39
2
  • 1
    So basically anything out of house should not be using 'using namespace'? But also you can't really know the future of your own code, so wouldn't it be better to just not use it at all? Commented Jan 20, 2018 at 14:49
  • 1
    Personally I find the advice not to use using namespace statements to be primarily applicable to header files. For source files I’d use them whenever practical. Admittedly that’s not very frequently. Commented Jan 21, 2018 at 0:43
3

The using namespace directive is useful when you have namespaces that identify your project.

Say you have a project that uses the developerIdentifier::projectName namespaces. That project has a class exampleClass and a set of utility functions under the namespace utils. The fully qualified names of these are developerIdentifier::projectName::exampleClass and developerIdentifier::projectName::utils.


You have the header stub exampleClass.hpp:

#include "utils.hpp"
namespace developerIdentifier {
namespace projectName {
class exampleClass
{
public:
 void function1();
 void function2();
};
}
}

The matching implementation file exampleClass.cpphas using namespace developerIdentifier::projectName; written at the top:

using namespace developerIdentifier::projectName;
void exampleClass::function1() { return utils::frobnicate(1, 2); };
void exampleClass::function2() {};

Because this using namespace declaration is in the implementation file it won't pollute the namspace of any classes importing exampleClass.hpp.

This declaration has two benefits:

  1. You don't have to write fully qualified function signatures like void developerIdentifier::projectName::exampleClass::function1() within exampleClass.cpp.
  2. You don't have to fully qualify developerIdentifier::projectName::utils::frobnicate() within function1().
answered Jan 20, 2018 at 17:59
1
  • 2
    Well, there's nothing stopping you from using the same pattern in the implementation-file as in the header... Commented Jan 20, 2018 at 21:22

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.