C ++ Engineering Practice (1): use anonymous namespace with caution

Source: Internet
Author: User

Anonymous namespace (anonymous namespace or unnamed namespace) is a very useful function of C ++. Its main purpose is to make members (variables or functions) in the namespace) it has a unique global name to avoid name collision (name collisions ). When writing a. cpp file, if you need to write some small helper functions, we usually put them in the anonymous namespace. In muduo 0.1.7, muduo/base/date. CC and muduo/base/thread. CC use the anonymous namespace.

I recently encountered this problem with my new thoughts and found that anonymous namespace is not a lot of advantages.

Two usage methods of static keywords in C Language

The static keyword of C language has two purposes:

1. It is used to modify the variable inside the function, that is, the static variable in the function. The lifetime of this variable is longer than that of this function, so that the function has a certain "State ". Functions that use static variables are generally not reentrant, nor thread-safe.

2. When a variable or function is modified at the file level (outside the function body), it indicates that the variable or function is visible only in this file. Other files cannot see or access the variable or function. The professional statement is "having internal linkage" (in short, it is not exposed to other translation units ).

The usage of C language is clear and generally not confusing.

Four usage of static keywords in C ++

Because c ++ introduces class, while maintaining compatibility with C language, the static keyword has two new usage methods:

3. It is used to modify the data member of the class, that is, the so-called "static member ". The lifetime of the data member is greater than the object (entity instance) of the class ). Static data members have one copy for each class, and common data members have one copy for each instance. Therefore, they are also called class variable and instance variable.

4. The member function used to modify the class, that is, the so-called "static member function ". This type of member function can only access class variable and other staticProgramFunction. You cannot access instance variable or instance method.

Of course, these usage types can be combined with each other. For example, the member functions of C ++ (both static and instance) can have their local static variables (usage 1 above ). For the class template and function template, the real number of static objects is related to the template instantiation (the template is now available). I believe that those who have learned the C ++ template will not be unfamiliar.

It can be seen that static is overloaded multiple times in C ++. The introduction of anonymous namespace is to reduce the burden on static. It replaces the 2nd usage of static. That is to say, the file-level static keyword is not required in C ++. We can use anonymous namespace to achieve the same effect. (In fact, the linkage may be slightly different. We will not discuss it here .)

Disadvantages of anonymous namespace

In engineering practice, anonymous namespace has two major disadvantages:

    1. It is difficult to set breakpoints for functions. If you use text mode debugger like GDB.
    2. When g ++ is used in some versions, the binary files compiled by the same file change each time, which causes some build tools to fail.

Consider the following shortCode(Anon. CC ):

 
1: Namespace

 
2:{

 
3:VoidFoo ()

 
4:{

 
5:}

 
6:}

 
7:

 
8: IntMain ()

9:{

 
10:Foo ();

 
11:}

For Question 1:

The <tab> auto-completion function of GDB helps us set breakpoints, which is not a big problem. The premise is that you know that"(Anonymous namespace): Foo ()"Exactly the function you want.

$ GDB./A. Out
Gnu gdb (GDB) 7.0.1-Debian

(GDB) B' <Tab>
(Anonymous namespace) _ data_start _ end
(Anonymous namespace): Foo () _ Do_global_ctors_aux _ fini
_ Dynamic _ do_global_dtors_aux _ init
_ Global_offset_table _ dso_handle _ start
_ Io_stdin_used _ gxx_personality_v0 anon. CC
_ Ctor_end _ gxx_personality_v0 @ PLT call_gmon_start
_ Ctor_list _ init_array_end completed.6341
_ Dtor_end _ init_array_start data_start
_ Dtor_list _ libc_csu_fini dtor_idx.6343
_ Frame_end _ libc_csu_init foo
_ Jcr_end _ libc_start_main frame_dummy
_ Jcr_list _ libc_start_main @ PLT int
_ Bss_start _ edata main

(GDB) B '(<Tab>
Anonymous namespace): Foo ()

(GDB) B '(anonymous namespace): Foo ()'
Breakpoint 1 at 0x400588: file anon. CC, line 4.

The trouble is that if two files are anon. CC and anonlib. CC defines the Foo () function in the anonymous space (this will not conflict with each other), so GDB cannot distinguish the two functions. You can only set a breakpoint for one of them. Or you can useFile Name: row numberTo set breakpoints separately. (Technically, the function in the anonymous namespace is weak text. Linker will not report an error if there is a duplicate name in the Link .)

The fundamental solution is to use a common named namespace. If you are afraid of duplicate names, you can use the source file name (add the path if necessary) as part of the namespace name.

For Question 2:

Compile it twice to generate a. Out and B. out respectively:

$G ++-g-o a. Out anon. CC

$G ++-g-o B. Out anon. CC

$Md5sum A. Out B. Out
0f7a9cc15af7ab1e57af17ba16afcd70 A. Out
8f22fc2bbfc27beb922aefa97d174e3b B. Out

$G ++ -- version
G ++ (GCC) 4.2.4 (Ubuntu 4.2.4-1ubuntu4)

$ diff-u <(nm. out) <(nm B. out)
---/dev/FD/63 22:27:58. 960754999 + 0800
++/dev/FD/62 22:27:58. 960754999 + 0800
@-0000000000600940 + 0000000000400634 @
D _ global_offset_table _
r _ io_stdin_used
W _ jv_registerclasses
-0000000000400538 T _ zn36_global1_n _ anon. cc_00000000_e2ceeb513fooev
+ 0000000000400538 T _ zn36_global1_n _ anon. cc_00000000_cb51498d3fooev
0000000000600748 D _ ctor_end _
0000000000600740 D _ ctor_list _
0000000000600758 D _ dtor_end _

As shown in the preceding figure, G ++ 4.2.4 randomly generates a unique name (e2ceeb51 and cb51498d In the mangled name of the Foo () function for the anonymous namespace ), to ensure that the name does not conflict. That is to say, the two compiled binary files of the same source file are different, which sometimes causes problems. For example, if a binary executable file with core dump is obtained, it cannot be determined which revision code is compiled. After all, the compilation results cannot be reproduced and are random.

This can be solved using the-frandom-seed parameter of GCC. For details, see the document.

This phenomenon exists in GCC 4.2.4 (the previous version is estimated to be similar) and does not exist in GCC 4.4.5.

Alternative

If the previous "disadvantage" brings you troubles, the solution is very simple, that is, using a common namespace. Of course, a good name is required. For example, boost: detail is often used in boost to put functions or classes that "should not be exposed to customers, but must be put in header files.

All in all, there is no major problem with anonymous namespace, and it is not a fault to use it. In case it gets in the way, you can replace it with a normal namespace.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.