C ++ Correct position for 'use of namespace'

I integrated some C ++ into somelse one and found out that we accept two different strategies for using commands using namespace .

For clean source code, which of the two is the best solution?

namespace foo { using namespace bar; } 

or

 using namespace bar; namespace foo { } 

Many thanks for your help,

T.

+5
source share
2 answers

These two are not equivalent. In the first case, the bar namespace is imported into the foo namespace, so for each bar::x you can access it as foo::x . In the last namespace, bar imported into the global namespace (or the namespace that wraps both), and can be accessed as ::x .

I would recommend always choosing the narrowest solution for you. Even in terms of including the namespace only in the function that you really need. Therefore, if in doubt, go to the first.

+7
source

This question has already been answered, but I want to repeat the point discussed by Toby Speight . You cannot write something like using namespace bar .

It is good practice to use using as anti-aliasing for long / hierarchical namespaces . how

 using po = boost::program_options; 

And then use

 po.notify(vm); 

Any of the two alternatives in question is suitable only for discussions or interviews.

0
source

Source: https://habr.com/ru/post/1234043/


All Articles