Is it good to use multiple namespaces in the same C ++ source file?

I want to include more than one namespace in the same .cpp file.

While std widely used, the z3 namespace will be used in approximately 10% of the 25 KLOC files.

Would it be good practice to use both as

  using namespace std; using namespace z3; 

I am thinking of using only std and then using z3 methods, specifying the name of the slot if necessary. How,

  using namespace std; z3::context c; z3::solver s; 

Which one is better?

I do not want to rename them to one namespace.

Thank you and welcome, Sukanya

+5
source share
2 answers

In fact, it’s best not to import the entire namespace into your program because it pollutes your namespace. This can lead to name collisions. It’s best to import only what you use.

So, instead of:

 using namespace z3; 

You should:

 using z3::context; 
+9
source

You can also use namespace , where you really need it, inside the function body, for example:

 void foo() { using namespace std; using z3::context; // some implementation }; 
0
source

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


All Articles