Simple understanding of templates and overloads

Posted by tappy on Sun, 02 Jan 2022 23:40:58 +0100

  1. explain

    • angle

      • Introduce templates and overloads from the perspective of compilation
    • Unique symbol

      • The default functions are strongly signed, and global, unless explicitly specified
  2. C + + overloading

    • Overload concept

      • The function parameters are different and the function names are the same
    • compiler

      • Guarantee uniqueness

        • Executable files generally require global uniqueness for symbol (function or variable) parsing
        • You can't have two of the same, otherwise there will be ambiguity, which is not allowed in the program
      • How?

        • Review the highlights of overloading: different function parameters and the same function name
        • C + + compiler is a magic change of symbols
      • code

        void show()
        {
        }
        void show(int a)
        {
            
        }
        int main()
        {
           show();
        }
        
        
        • Pay attention to comparing the decompiled results.
      • Decompile result

        • Compile: G + + - C test cpp -fno-builtin
        [root@localhost cppcompile]# objdump -rd test.o
        
        test.o:     file format elf64-x86-64
        
        
        Disassembly of section .text:
        
        0000000000000000 <_Z4showv>:
          0:   55                      push   %rbp
          1:   48 89 e5                mov    %rsp,%rbp
          4:   5d                      pop    %rbp
          5:   c3                      retq
        
        0000000000000006 <_Z4showi>:
          6:   55                      push   %rbp
          7:   48 89 e5                mov    %rsp,%rbp
          a:   89 7d fc                mov    %edi,-0x4(%rbp)
          d:   5d                      pop    %rbp
          e:   c3                      retq
        
        000000000000000f <main>:
          f:   55                      push   %rbp
         10:   48 89 e5                mov    %rsp,%rbp
         13:   e8 00 00 00 00          callq  18 <main+0x9>
                               14: R_X86_64_PC32       _Z4showv-0x4
         18:   b8 00 00 00 00          mov    $0x0,%eax
         1d:   5d                      pop    %rbp
         1e:   c3                      retq
        
        
        • The generated symbol is changed by magic, and show becomes_ Z4showv and_ Z4showi.
        • Corresponding to void show(void) and void show(int)
        • _ Z is a uniform prefix, and 4 indicates the actual length of the function name show original function name v indicates type i represents the corresponding i type
      • notes

        • Different return value types are not overloaded because the compiler does not process the return value
      • summary

        • Therefore, the concept of overloading is the magic change of symbols to achieve the only difference between symbols
  3. Template

    • explain

      • Templates are similar to overloads
    • Template symbol

      • generative principle

        • If used, the compiler generates a corresponding function (instantiation), and the generation is completed by the compiler
      • De duplication principle

        • section is the unit of weight removal
        • section note: data,.text, the so-called data area, code area and so on The linker is responsible for de duplication
    • case

      • code

        template <typename T>
        void show(T a)
        {
        }
        int main()
        {
           show<float>(1);
           show<int>(1);
        }
        
        
      • Decompile

        • Compile: G + + - C test cpp -fno-builtin
        [root@localhost cppcompile]# objdump -rd test.o
        
        test.o:     file format elf64-x86-64
        
        
        Disassembly of section .text:
        
        0000000000000000 <main>:
          0:   55                      push   %rbp
          1:   48 89 e5                mov    %rsp,%rbp
          4:   f3 0f 10 05 00 00 00    movss  0x0(%rip),%xmm0        # c <main+0xc>
          b:   00
                               8: R_X86_64_PC32        .rodata-0x4
          c:   e8 00 00 00 00          callq  11 <main+0x11>
                               d: R_X86_64_PC32        _Z4showIfEvT_-0x4
         11:   bf 01 00 00 00          mov    $0x1,%edi
         16:   e8 00 00 00 00          callq  1b <main+0x1b>
                               17: R_X86_64_PC32       _Z4showIiEvT_-0x4
         1b:   b8 00 00 00 00          mov    $0x0,%eax
         20:   5d                      pop    %rbp
         21:   c3                      retq
        
        Disassembly of section .text._Z4showIfEvT_:
        
        0000000000000000 <_Z4showIfEvT_>:
          0:   55                      push   %rbp
          1:   48 89 e5                mov    %rsp,%rbp
          4:   f3 0f 11 45 fc          movss  %xmm0,-0x4(%rbp)
          9:   5d                      pop    %rbp
          a:   c3                      retq
        
        Disassembly of section .text._Z4showIiEvT_:
        
        0000000000000000 <_Z4showIiEvT_>:
          0:   55                      push   %rbp
          1:   48 89 e5                mov    %rsp,%rbp
          4:   89 7d fc                mov    %edi,-0x4(%rbp)
          7:   5d                      pop    %rbp
          8:   c3                      retq
        
        
        • The disassembly of section here text._Z4showIiEvT_: The description is a new section named text._Z4showIiEvT_.
        • Here is only one analogy, another analogy Textdescription is the code area (functions are basically placed in the code area) However, it is mainly up to the compiler to decide where to generate Different compilers may be different
        • .text._Z4showIiEvT_, Here is the instantiation of show < int > (1)
        • _ Z same prefix, 4 length, show actual function, I unified format, I represents int type, Evt_ Uniform format
      • Summary

        • The symbol is unique here, but how to merge different partitions?
        • I need two o can only be reflected. It involves more content, so I won't introduce it
  4. Summary

    • Reference books

      • Self cultivation of programmers
      • <linker and loader>
    • expand

      • Namespace and class are similar
      • class has some differences for virtual tables

Topics: C++