-
总览
-
生成
-
按需生成
-
生成拷贝构造
[root@localhost test]# g++ test.cpp -std=c++11 -g [root@localhost test]# gdb ./a.out -q Reading symbols from /root/cppfile/test/a.out...done. (gdb) disassemble /r main Dump of assembler code for function main(): 0x00000000004007bd <+0>: 55 push %rbp 0x00000000004007be <+1>: 48 89 e5 mov %rsp,%rbp 0x00000000004007c1 <+4>: 53 push %rbx 0x00000000004007c2 <+5>: 48 83 ec 28 sub $0x28,%rsp 0x00000000004007c6 <+9>: 48 8d 45 e0 lea -0x20(%rbp),%rax 0x00000000004007ca <+13>: 48 89 c7 mov %rax,%rdi 0x00000000004007cd <+16>: e8 6e 00 00 00 callq 0x400840 <T::T()> 0x00000000004007d2 <+21>: 48 8d 45 d0 lea -0x30(%rbp),%rax 0x00000000004007d6 <+25>: 48 89 c7 mov %rax,%rdi 0x00000000004007d9 <+28>: e8 62 00 00 00 callq 0x400840 <T::T()> 0x00000000004007de <+33>: 48 8d 55 d0 lea -0x30(%rbp),%rdx 0x00000000004007e2 <+37>: 48 8d 45 e0 lea -0x20(%rbp),%rax 0x00000000004007e6 <+41>: 48 89 d6 mov %rdx,%rsi 0x00000000004007e9 <+44>: 48 89 c7 mov %rax,%rdi 0x00000000004007ec <+47>: e8 d1 00 00 00 callq 0x4008c2 <T::operator=(T const&)> 0x00000000004007f1 <+52>: bb 00 00 00 00 mov $0x0,%ebx 0x00000000004007f6 <+57>: 48 8d 45 d0 lea -0x30(%rbp),%rax 0x00000000004007fa <+61>: 48 89 c7 mov %rax,%rdi 0x00000000004007fd <+64>: e8 a2 00 00 00 callq 0x4008a4 <T::~T()> 0x0000000000400802 <+69>: 48 8d 45 e0 lea -0x20(%rbp),%rax 0x0000000000400806 <+73>: 48 89 c7 mov %rax,%rdi 0x0000000000400809 <+76>: e8 96 00 00 00 callq 0x4008a4 <T::~T()> 0x000000000040080e <+81>: 89 d8 mov %ebx,%eax 0x0000000000400810 <+83>: eb 26 jmp 0x400838 <main()+123> 0x0000000000400812 <+85>: 48 89 c3 mov %rax,%rbx 0x0000000000400815 <+88>: 48 8d 45 d0 lea -0x30(%rbp),%rax 0x0000000000400819 <+92>: 48 89 c7 mov %rax,%rdi 0x000000000040081c <+95>: e8 83 00 00 00 callq 0x4008a4 <T::~T()> 0x0000000000400821 <+100>: 48 8d 45 e0 lea -0x20(%rbp),%rax 0x0000000000400825 <+104>: 48 89 c7 mov %rax,%rdi 0x0000000000400828 <+107>: e8 77 00 00 00 callq 0x4008a4 <T::~T()> 0x000000000040082d <+112>: 48 89 d8 mov %rbx,%rax 0x0000000000400830 <+115>: 48 89 c7 mov %rax,%rdi 0x0000000000400833 <+118>: e8 78 fe ff ff callq 0x4006b0 <_Unwind_Resume@plt> 0x0000000000400838 <+123>: 48 83 c4 28 add $0x28,%rsp 0x000000000040083c <+127>: 5b pop %rbx 0x000000000040083d <+128>: 5d pop %rbp 0x000000000040083e <+129>: c3 retq End of assembler dump. (gdb) break main Breakpoint 1 at 0x4007c6: file test.cpp, line 10. (gdb) r Starting program: /root/cppfile/test/./a.out Breakpoint 1, main () at test.cpp:10 10 T a; (gdb) info source Current source file is test.cpp Compilation directory is /root/cppfile/test Located in /root/cppfile/test/test.cpp Contains 14 lines. Source language is c++. Compiled with DWARF 2 debugging format. Does not include preprocessor macro info. (gdb) !cat /root/cppfile/test/test.cpp #include<string> class T { public: int a; int b; std::string c{"122345"}; }; int main() { T a; T b; a = b; return 0; }
-
默认拷贝构造
[root@localhost test]# g++ test.cpp -std=c++11 -g [root@localhost test]# gdb ./a.out -q Reading symbols from /root/cppfile/test/a.out...done. (gdb) break main Breakpoint 1 at 0x4007c6: file test.cpp, line 10. (gdb) r Starting program: /root/cppfile/test/./a.out Breakpoint 1, main () at test.cpp:10 10 T a; (gdb) disassemble main Dump of assembler code for function main(): 0x00000000004007bd <+0>: push %rbp 0x00000000004007be <+1>: mov %rsp,%rbp 0x00000000004007c1 <+4>: push %rbx 0x00000000004007c2 <+5>: sub $0x28,%rsp => 0x00000000004007c6 <+9>: lea -0x20(%rbp),%rax 0x00000000004007ca <+13>: mov %rax,%rdi 0x00000000004007cd <+16>: callq 0x400828 <T::T()> 0x00000000004007d2 <+21>: lea -0x20(%rbp),%rdx 0x00000000004007d6 <+25>: lea -0x30(%rbp),%rax 0x00000000004007da <+29>: mov %rdx,%rsi 0x00000000004007dd <+32>: mov %rax,%rdi 0x00000000004007e0 <+35>: callq 0x4008aa <T::T(T const&)> 0x00000000004007e5 <+40>: mov $0x0,%ebx 0x00000000004007ea <+45>: lea -0x30(%rbp),%rax 0x00000000004007ee <+49>: mov %rax,%rdi 0x00000000004007f1 <+52>: callq 0x40088c <T::~T()> 0x00000000004007f6 <+57>: lea -0x20(%rbp),%rax 0x00000000004007fa <+61>: mov %rax,%rdi 0x00000000004007fd <+64>: callq 0x40088c <T::~T()> 0x0000000000400802 <+69>: mov %ebx,%eax 0x0000000000400804 <+71>: jmp 0x400820 <main()+99> 0x0000000000400806 <+73>: mov %rax,%rbx 0x0000000000400809 <+76>: lea -0x20(%rbp),%rax 0x000000000040080d <+80>: mov %rax,%rdi 0x0000000000400810 <+83>: callq 0x40088c <T::~T()> 0x0000000000400815 <+88>: mov %rbx,%rax 0x0000000000400818 <+91>: mov %rax,%rdi 0x000000000040081b <+94>: callq 0x4006c0 <_Unwind_Resume@plt> 0x0000000000400820 <+99>: add $0x28,%rsp 0x0000000000400824 <+103>: pop %rbx 0x0000000000400825 <+104>: pop %rbp 0x0000000000400826 <+105>: retq End of assembler dump. (gdb) !cat test.cpp #include<string> class T { public: int a; int b; std::string c{"122345"}; }; int main() { T a; T b(a); return 0; }
-
反汇编
0000000000400828 <_ZN1TC1Ev>: 000000000040088c <_ZN1TD1Ev>: 00000000004008aa <_ZN1TC1ERKS_>:
[root@localhost test]# c++filt _ZN1TC1Ev _ZN1TD1Ev _ZN1TC1ERKS_ T::T() T::~T() T::T(T const&)
-
移动须知
-
智能移动
[root@localhost test]# make g++ test.cpp -std=c++11 -g gdb ./a.out -q -ex 'break main' -ex 'r' -ex 'disassemble main' -ex 'c' -ex '!cat test.cpp' Reading symbols from /root/cppfile/test/a.out...done. Breakpoint 1 at 0x400a86: file test.cpp, line 27. Starting program: /root/cppfile/test/./a.out Breakpoint 1, main () at test.cpp:27 27 T a; Dump of assembler code for function main(): 0x0000000000400a7d <+0>: push %rbp 0x0000000000400a7e <+1>: mov %rsp,%rbp 0x0000000000400a81 <+4>: push %rbx 0x0000000000400a82 <+5>: sub $0x48,%rsp => 0x0000000000400a86 <+9>: lea -0x30(%rbp),%rax 0x0000000000400a8a <+13>: mov %rax,%rdi 0x0000000000400a8d <+16>: callq 0x400b9e <T::T()> 0x0000000000400a92 <+21>: lea -0x50(%rbp),%rax 0x0000000000400a96 <+25>: mov %rax,%rdi 0x0000000000400a99 <+28>: callq 0x400b9e <T::T()> 0x0000000000400a9e <+33>: lea -0x50(%rbp),%rax 0x0000000000400aa2 <+37>: mov %rax,%rdi 0x0000000000400aa5 <+40>: callq 0x400c40 <std::move<T&>(T&)> 0x0000000000400aaa <+45>: mov %rax,%rdx 0x0000000000400aad <+48>: lea -0x30(%rbp),%rax 0x0000000000400ab1 <+52>: mov %rdx,%rsi 0x0000000000400ab4 <+55>: mov %rax,%rdi 0x0000000000400ab7 <+58>: callq 0x400c4e <T::operator=(T&&)> 0x0000000000400abc <+63>: mov $0x0,%ebx 0x0000000000400ac1 <+68>: lea -0x50(%rbp),%rax 0x0000000000400ac5 <+72>: mov %rax,%rdi 0x0000000000400ac8 <+75>: callq 0x400c12 <T::~T()> 0x0000000000400acd <+80>: lea -0x30(%rbp),%rax 0x0000000000400ad1 <+84>: mov %rax,%rdi 0x0000000000400ad4 <+87>: callq 0x400c12 <T::~T()> 0x0000000000400ad9 <+92>: mov %ebx,%eax 0x0000000000400adb <+94>: jmp 0x400b03 <main()+134> 0x0000000000400add <+96>: mov %rax,%rbx 0x0000000000400ae0 <+99>: lea -0x50(%rbp),%rax 0x0000000000400ae4 <+103>: mov %rax,%rdi 0x0000000000400ae7 <+106>: callq 0x400c12 <T::~T()> 0x0000000000400aec <+111>: lea -0x30(%rbp),%rax 0x0000000000400af0 <+115>: mov %rax,%rdi 0x0000000000400af3 <+118>: callq 0x400c12 <T::~T()> 0x0000000000400af8 <+123>: mov %rbx,%rax 0x0000000000400afb <+126>: mov %rax,%rdi 0x0000000000400afe <+129>: callq 0x400980 <_Unwind_Resume@plt> 0x0000000000400b03 <+134>: add $0x48,%rsp 0x0000000000400b07 <+138>: pop %rbx 0x0000000000400b08 <+139>: pop %rbp 0x0000000000400b09 <+140>: retq End of assembler dump. Continuing. copy assign [Inferior 1 (process 11575) exited normally] #include<string> #include<iostream> class D { public: D(){} ~D(){} D(D&&){} D(const D&){} D& operator=(const D&){ std::cout << "copy assign" << std::endl; } }; class T { public: int a; int b; std::string c{"122345"}; D d; T(){} T(const T&){} T& operator=(T&&)=default; }; int main() { T a; T b; a = std::move(b); return 0; }
-
智能移动并不都是移动,也有可能是拷贝.
-
-
影响
-
C++
编译器演进 -
使用默认
-
总结
C++11 类的六大基本成员函数默认生成,default字段的含义
最新推荐文章于 2023-02-04 01:29:53 发布