How to use the "using" keyword in C ++

I am a bit confused as to which one is the best way to use the using C ++ keyword for namespaces. Suppose the following code is in the backtrace.h header file

 #include <memory> using my_namespace1::component1; using my_namespace2::component2; namespace my_application { namespace platform_logs { class backtrace_log { //code that creates instances of my_namespace1::component1 and my_namespace2::component2 }; } } 

OR

 #include <memory> namespace my_application { namespace platform_logs { using my_namespace1::component1; using my_namespace2::component2; class backtrace_log { //code that creates instances of my_namespace1::component1 and my_namespace2::component2 }; } } 

which one is better and why?

+5
source share
3 answers

In the header file ... the following example: evil . Never use using namespace... in the global header area. This forces a lot of user characters not specified for characters, which can make it very difficult to fix problems if they come across other headers.

 #include <memory> using my_namespace1::component1; using my_namespace2::component2; namespace my_application { namespace platform_logs { class backtrace_log { //code that creates instances of my_namespace1::component1 and my_namespace2::component2 }; } } 

On the other hand, this next example is good, but should be used with caution. It makes sense if you want to include all of these characters in API This often happens with internal project namespaces, but less likely with third-party namespaces.

I would especially avoid even this with very large namespaces like std:: .

 #include <memory> namespace my_application { namespace platform_logs { using my_namespace1::component1; using my_namespace2::component2; class backtrace_log { //code that creates instances of my_namespace1::component1 and my_namespace2::component2 }; } } 
+4
source

You should always draw characters from other namespaces in the narrowest possible area (so as not to contaminate external namespaces), and, as a rule, pull out only the characters you need, and not just the entire namespace.

In headings (which can be included by many users) you should usually refrain from alltogether practice and instead always prefer to simply use explicit types of namespace types.

+8
source

In the first example, when you turn on

  backtrace.h 

in another file, this file will also use these names. Therefore, if by mistake someone wrote "blax" in the file where you included backtrace.h, and "blax" also turned out to be a class or function in your namespace, for example:

 my_namespace1::component1::blax 

the compiler can use its "blax" to mean "my_namespace1 :: component1 :: blax"

By placing the using namespace in another namespace, you simply include all these definitions in this namespace, in the previous example, if you went with the second version of the code, which will not collide because "my_namespace1 :: component1 :: blax" will be included as my_application :: platform_logs :: blax.

In general, most coding guides will help you: a) Prefer to never do "use namespaces" or at least use it only for shorthand (for example, using short_namespace = first_namespace :: another_namepsace :: last_namespace)

b) If you use "use namespace", do it in the source file (for example, ccp or .c files), since the definitions in these files are not included

c) If you use "use namespace" in headers, paste it into another namespace (for example, in your example) so that it does not "leak" into the scope of another file that includes your header

+2
source

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


All Articles