Best way to use 'using'

Apr 19, 2018 at 10:04am
Hello,

I would like to know the best way to use the 'using' keyword. I am aware that there are topics (especially on stackoverflow), but unfortunately I still haven't found the answer to my question.

Here is what I understood:
- never use namespace std
- it really isn't recommended to use it in headers

What I think:
- writing the whole namespace when calling or defining a member function helps readability (we know from where it comes from and we are sure to avoid naming conflicts)
- I know that with this method also means writing the whole namespace(s)

So I would like to know if what I think / understood or read is correct.

Thank you very much in advance for all your answers.
Apr 19, 2018 at 10:45am
You seem to be talking about the directive.

There is more to using than that: http://en.cppreference.com/w/cpp/keyword/using

More insight on the topic: http://www.gotw.ca/gotw/053.htm
Apr 19, 2018 at 11:03am
Thank you very much for answer.

I now know what I am talking about.

So does using the using directive cause a problem ? Can it be evil ?

Also concerning the use of a base constructor
1
2
3
4
5
6
7
8
9
class foo {
    public:
       foo();
};

class bar : public foo {
    public:
       using foo::foo;
};


Since it was introduced in c++11 is it okay to use it ?

Thank you very much in advance.
Last edited on Apr 19, 2018 at 11:06am
Apr 19, 2018 at 8:38pm
Hi BigfootCpp,

The "using namespace..." permits to flatten a namespace in your current source code.
This is handy but then comes some "decision" (automatic decision) making when identifier collide.
Say you have a constant pi in your program and you flatten a namespace with using, ant that namespace also defines a "pi" identifier. What happens ?
The rules are clear ok but when you do that, you open your source code to possibly have that issue for any and every identifier (variable, function...).

The namespace "std" is so fondamental, that's the reason why it is so short : only three letters... that means "guyz, don't be that lazy and prefix with 5 letters" that is std::

But most importantly, when you use a "using namespace..." in a header file, you will "pollute" every header file and implementation file that will include your header.

For all of that, here are the rules I apply :
1) if an implementation file is really and exceptionnaly std:: use intensive, then I may use the "using namespace std;" on a global scope... it never happened anyway until now. But that's acceptable to me.
2) when "needed", I use the "using namespace" locally to a function... this way I never pollute my entire program and this can be handy.
1
2
3
4
5
void aFunction()
{
	using namespace ...;
}
// here, the using namespace has no effect 


Hope that helps ^_^
Last edited on Apr 19, 2018 at 8:41pm
Apr 19, 2018 at 9:30pm
Thank you very precise much for your answer. It really helped me.

I was also very pleased by your rules. I now have good practices concerning the "using namespace".

Thank you.
Apr 20, 2018 at 1:27am
My personal preference is to never use using declarations at namespace scope nor block scope except when required to enable argument-dependent lookup. For instance, in a generic program that swaps two elements, we might (should) write
1
2
3
4
5
{ 
  // ...
  using std::swap; 
  swap(a, b); 
}

so that we may benefit from a custom swap function if one is available.

Otherwise, I don't recommend using namespace to beginners - or anyone, really - because not everyone knows the exact contents of the namespace they're importing. Accidentally using a name introduced by it asks for trouble.

In the stdlib, for example, the (standard) names introduced by any given header is implementation-dependent. This means we need to have the entire stdlib in mind when writing code that uses the std namespace, lest we write a potential name collision. Failing to do so results in situations where code works correctly on one machine but silently calls the wrong function on another (the professor's, usually), and that's just no fun.

It's certainly fine to alias namespace names, but I would usually advise against using names or namespaces in all but the most limited cases. We usually don't want ADL and never want name collisions, so in my opinion it's best to avoid it when possible. YMMV.
Last edited on Apr 20, 2018 at 1:31am
Apr 20, 2018 at 6:34am
Thank you for your very detailed and neat response.

It's great to have another point of view.

Thank you for your help :)
Apr 20, 2018 at 4:09pm
Some people advocate for adding std:: in front of any symbol in the namespace because it clarifies which symbol you want. This is a sound reasoning.

Personally, I like terse code so when using name from std, I put using std::name; at the top of my implementation file. That way I can refer to it with name but I don't drag in the entire namespace.

Of all the "using namespace" sins, I think that putting it in a header file is worst. You aren't just asking for trouble, you're begging for it.
Apr 20, 2018 at 9:28pm
Thank you for your answer and help :)
Topic archived. No new replies allowed.