码迷,mamicode.com
首页 > 编程语言 > 详细

《深度探索C++对象模型》之《构造函数语义学》章节探索与理解

时间:2019-10-16 23:37:03      阅读:131      评论:0      收藏:0      [点我收藏+]

标签:reference   declare   详细   重点   enc   dump   strong   binary   tor   

构造函数语义学

这一章原书主要分析了:编译器关于对象构造过程的干涉,即在对象构造这个过程中,编译器到底在背后做了什么

这一章的重点在于default constructor和copy constructor的相关知识

Default Constructor

一些C++的书籍中告诉我们:如果不写任何构造函数,那么编译器会为我们生成一个默认构造函数(default constructor)

那么,针对这个观点,有两个问题:

  • 这句话是正确的吗?
  • 如果这句话是正确的,那么编译器为我们生成的default constructor里有什么动作吗?

1 这句话是正确的吗?

这句话大体是正确的,但是cppreference中有更详细的说明:

隐式声明的默认构造函数
若不对类类型(struct、class?或?union)提供任何用户声明(user-declared)的构造函数,则编译器将始终声明一个作为其类的?inline public?成员的默认构造函数

隐式定义的默认构造函数
若隐式声明的默认构造函数未被定义为弃置的,则当其被ODR 式使用时,它为编译器所定义(即生成函数体并编译之),且它与拥有空函数体和空初始化器列表的用户定义的构造函数有严格相同的效果。即它调用这个类的各基类和各非静态成员的默认构造函数

有没有什么证据能证明编译器真的隐式定义了一个默认构造函数呢?有,考虑如下的一个类:

class A {};

这个类中没有任何成员变量或者成员函数,此时打印出该类的相关信息如下:

// clang -cc1 -fdump-record-layouts -emit-llvm main.cpp 命令
*** Dumping AST Record Layout
         0 | class A (empty)
           | [sizeof=1, dsize=1, align=1,
           |  nvsize=1, nvalign=1]

*** Dumping IRgen Record Layout
Record: CXXRecordDecl 0x55c6626abc70 <main.cpp:1:1, line:3:1> line:1:7 referenced class A definition
|-DefinitionData pass_in_registers empty aggregate standard_layout trivially_copyable pod trivial literal has_constexpr_non_copy_move_ctor can_const_default_init
| |-DefaultConstructor exists trivial constexpr defaulted_is_constexpr 
| |-CopyConstructor simple trivial has_const_param implicit_has_const_param
| |-MoveConstructor exists simple trivial
| |-CopyAssignment trivial has_const_param needs_implicit implicit_has_const_param
| |-MoveAssignment exists simple trivial needs_implicit
| `-Destructor simple irrelevant trivial needs_implicit
|-CXXRecordDecl 0x55c6626abd88 <col:1, col:7> col:7 implicit class A
|-CXXConstructorDecl 0x55c6626ac020 <col:7> col:7 implicit used constexpr A 'void () noexcept' inline default trivial
| `-CompoundStmt 0x55c6626ac4b8 <col:7>
|-CXXConstructorDecl 0x55c6626ac158 <col:7> col:7 implicit constexpr A 'void (const A &)' inline default trivial noexcept-unevaluated 0x55c6626ac158
| `-ParmVarDecl 0x55c6626ac268 <col:7> col:7 'const A &'
`-CXXConstructorDecl 0x55c6626ac308 <col:7> col:7 implicit constexpr A 'void (A &&)' inline default trivial noexcept-unevaluated 0x55c6626ac308
  `-ParmVarDecl 0x55c6626ac418 <col:7> col:7 'A &&'

Layout: <CGRecordLayout
  LLVMType:%class.A = type { i8 }
  NonVirtualBaseLLVMType:%class.A = type { i8 }
  IsZeroInitializable:1
  BitFields:[
]>

DefaultConstructor exists trivial constexpr defaulted_is_constexpr这一行证明了这个类存在一个default constructor且是trivial的,而我们并没有声明和定义,那么自然是编译器为我们自动生成的

2 隐式定义的默认构造函数里发生了什么?

在解决这个问题之前,首先有需要了解一些额外的知识:

default constructor是分为trivial default constructor(平凡默认构造函数,即不进行任何构造动作)和non trivial default constructor(非平凡默认构造函数)

什么情况下default constructor是trivial的呢?cppreference中说明了这一点:

当下列各项全部为真时,类 T 的默认构造函数为平凡的(平凡默认构造函数是不进行任何动作的构造函数。):
构造函数并非用户提供(即为隐式定义或于其首个声明中预置的)
T 没有虚成员函数
T 没有虚基类
T 没有拥有默认初始化器的非静态数据成员。
每个 T 的直接基类都拥有平凡默认构造函数
每个类类型的非静态成员都拥有平凡默认构造函数

注意上述条件中的第一点也说明了一个事实:如果构造函数是用户定义的,那么必定是非平凡的

再回到隐式定义的默认构造函数里发生了什么?这个问题,就需要从两个角度来考虑了:

  1. 当编译器隐式定义的默认构造函数是一个trivial default constructor时,那么该trivial default constructor什么也不做
  2. 那么当编译器隐式定义的默认构造函数是一个non-trivial default constructor时,该non-trivial default constructor调用这个类的各基类和各非静态成员的默认构造函数

通过以上,我们已经知道了编译器在特定情况下,真的会隐式定义一个default constructor,也知道了在哪些情况下产生的default constructor是trivial的,那么再来详细的讨论一下,什么情况下,编译器生成的default constructor是non-trivial的

  1. 带有default constructor的member class object
    如果一个class A没有任何的constructor,但是它内含一个member object,而该member object拥有non-trivial default constructor,那么编译器为这个class隐式定义的default constructor是non-trivial。为什么呢?因为编译器为该class A产生的default constructor里必须要调用member object的non-trivial default constructor,否则无法完整的构造class A的对象
    仍然用代码来证明:

     class B {
      private:
       int b;
      public:
       B() {}
     };
    
     class A {
      private:
       B b;
         int a;
     };

    打印出class A的相关信息,如下:

     *** Dumping AST Record Layout
              0 | class B
              0 |   int b
                | [sizeof=4, dsize=4, align=4,
                |  nvsize=4, nvalign=4]
    
     *** Dumping AST Record Layout
              0 | class A
              0 |   class B b
              0 |     int b
              4 |   int a
                | [sizeof=8, dsize=8, align=4,
                |  nvsize=8, nvalign=4]
    
     *** Dumping IRgen Record Layout
     Record: CXXRecordDecl 0x55ba37562c50 <main.cpp:1:1, line:6:1> line:1:7 referenced class B definition
     |-DefinitionData pass_in_registers standard_layout trivially_copyable has_user_declared_ctor can_const_default_init
     | |-DefaultConstructor exists non_trivial user_provided
     | |-CopyConstructor simple trivial has_const_param implicit_has_const_param
     | |-MoveConstructor exists simple trivial
     | |-CopyAssignment trivial has_const_param needs_implicit implicit_has_const_param
     | |-MoveAssignment exists simple trivial needs_implicit
     | `-Destructor simple irrelevant trivial
     |-CXXRecordDecl 0x55ba37562d68 <col:1, col:7> col:7 implicit referenced class B
     |-AccessSpecDecl 0x55ba37562df8 <line:2:2, col:9> col:2 private
     |-FieldDecl 0x55ba37562e38 <line:3:3, col:7> col:7 b 'int'
     |-AccessSpecDecl 0x55ba37562e88 <line:4:2, col:8> col:2 public
     |-CXXConstructorDecl 0x55ba37562f38 <line:5:3, col:8> col:3 used B 'void ()'
     | `-CompoundStmt 0x55ba37562ff8 <col:7, col:8>
     |-CXXDestructorDecl 0x55ba37563590 <line:1:7> col:7 implicit ~B 'void ()' inline default trivial noexcept-unevaluated 0x55ba37563590
     |-CXXConstructorDecl 0x55ba375636c8 <col:7> col:7 implicit constexpr B 'void (const B &)' inline default trivial noexcept-unevaluated 0x55ba375636c8
     | `-ParmVarDecl 0x55ba375637d8 <col:7> col:7 'const B &'
     `-CXXConstructorDecl 0x55ba37563878 <col:7> col:7 implicit constexpr B 'void (B &&)' inline default trivial noexcept-unevaluated 0x55ba37563878
       `-ParmVarDecl 0x55ba37563988 <col:7> col:7 'B &&'
    
     Layout: <CGRecordLayout
       LLVMType:%class.B = type { i32 }
       NonVirtualBaseLLVMType:%class.B = type { i32 }
       IsZeroInitializable:1
       BitFields:[
     ]>
    
     *** Dumping IRgen Record Layout
     Record: CXXRecordDecl 0x55ba37563008 <main.cpp:8:1, line:12:1> line:8:7 referenced class A definition
     |-DefinitionData pass_in_registers standard_layout trivially_copyable
     | |-DefaultConstructor exists non_trivial
     | |-CopyConstructor simple trivial has_const_param implicit_has_const_param
     | |-MoveConstructor exists simple trivial
     | |-CopyAssignment trivial has_const_param needs_implicit implicit_has_const_param
     | |-MoveAssignment exists simple trivial needs_implicit
     | `-Destructor simple irrelevant trivial needs_implicit
     |-CXXRecordDecl 0x55ba37563128 <col:1, col:7> col:7 implicit class A
     |-AccessSpecDecl 0x55ba375631b8 <line:9:2, col:9> col:2 private
     |-FieldDecl 0x55ba375631f0 <line:10:3, col:5> col:5 b 'B'
     |-FieldDecl 0x55ba37563258 <line:11:3, col:7> col:7 a 'int'
     |-CXXConstructorDecl 0x55ba375634b0 <line:8:7> col:7 implicit used A 'void () noexcept(false)' inline default
     | |-CXXCtorInitializer Field 0x55ba375631f0 'b' 'B'
     | | `-CXXConstructExpr 0x55ba37592090 <col:7> 'B' 'void ()'
     | `-CompoundStmt 0x55ba375920e8 <col:7>
     |-CXXConstructorDecl 0x55ba375639f8 <col:7> col:7 implicit constexpr A 'void (const A &)' inline default trivial noexcept-unevaluated 0x55ba375639f8
     | `-ParmVarDecl 0x55ba37591e30 <col:7> col:7 'const A &'
     `-CXXConstructorDecl 0x55ba37591ec8 <col:7> col:7 implicit constexpr A 'void (A &&)' inline default trivial noexcept-unevaluated 0x55ba37591ec8
       `-ParmVarDecl 0x55ba37591fd8 <col:7> col:7 'A &&'
    
     Layout: <CGRecordLayout
       LLVMType:%class.A = type { %class.B, i32 }
       NonVirtualBaseLLVMType:%class.A = type { %class.B, i32 }
       IsZeroInitializable:1
       BitFields:[
     ]>

    DefaultConstructor exists non_trivial user_provided

    这一行中说明class B是用户定义的一个default constructor,是non-trivial的

    DefaultConstructor exists non_trivial

    第二行中则说明class A的default constructor也是存在的且是non-trivial的。在这个non-trivial default constructor中,编译器会调用class B的default constructor来初始化class A的对象中的b成分,但是不会初始化a(编译器看来,初始化b是它的工作,但是初始化a是程序员的工作)

    |-CXXConstructorDecl 0x55ba375634b0 col:7 implicit used A ‘void () noexcept(false)‘ inline default
    | |-CXXCtorInitializer Field 0x55ba375631f0 ‘b‘ ‘B‘
    | | -CXXConstructExpr 0x55ba37592090 <col:7> ‘B‘ ‘void ()‘ |-CompoundStmt 0x55ba375920e8

    这一段则说明了在class A的default constructor中调用了class B的default constructor

    如果,class A中提供了一个default constructor,那么编译器会怎么办呢?

     class B {
      private:
       int b;
      public:
       B() {}
     };
    
     class A {
      private:
       B b;
       int a;
      public:
       A() {
         a = 0;
       }
     };

    打印相关信息如下:

     *** Dumping AST Record Layout
              0 | class B
              0 |   int b
                | [sizeof=4, dsize=4, align=4,
                |  nvsize=4, nvalign=4]
    
     *** Dumping AST Record Layout
              0 | class A
              0 |   class B b
              0 |     int b
              4 |   int a
                | [sizeof=8, dsize=8, align=4,
                |  nvsize=8, nvalign=4]
    
     *** Dumping IRgen Record Layout
     Record: CXXRecordDecl 0x5641e5965d20 <main.cpp:1:1, line:6:1> line:1:7 referenced class B definition
     |-DefinitionData pass_in_registers standard_layout trivially_copyable has_user_declared_ctor can_const_default_init
     | |-DefaultConstructor exists non_trivial user_provided
     | |-CopyConstructor simple trivial has_const_param implicit_has_const_param
     | |-MoveConstructor exists simple trivial
     | |-CopyAssignment trivial has_const_param needs_implicit implicit_has_const_param
     | |-MoveAssignment exists simple trivial needs_implicit
     | `-Destructor simple irrelevant trivial
     |-CXXRecordDecl 0x5641e5965e38 <col:1, col:7> col:7 implicit referenced class B
     |-AccessSpecDecl 0x5641e5965ec8 <line:2:2, col:9> col:2 private
     |-FieldDecl 0x5641e5965f08 <line:3:3, col:7> col:7 b 'int'
     |-AccessSpecDecl 0x5641e5965f58 <line:4:2, col:8> col:2 public
     |-CXXConstructorDecl 0x5641e5966008 <line:5:3, col:8> col:3 used B 'void ()'
     | `-CompoundStmt 0x5641e59660c8 <col:7, col:8>
     |-CXXConstructorDecl 0x5641e59664e8 <line:1:7> col:7 implicit constexpr B 'void (const B &)' inline default trivial noexcept-unevaluated 0x5641e59664e8
     | `-ParmVarDecl 0x5641e59665f8 <col:7> col:7 'const B &'
     |-CXXConstructorDecl 0x5641e5966698 <col:7> col:7 implicit constexpr B 'void (B &&)' inline default trivial noexcept-unevaluated 0x5641e5966698
     | `-ParmVarDecl 0x5641e59667a8 <col:7> col:7 'B &&'
     `-CXXDestructorDecl 0x5641e59953b8 <col:7> col:7 implicit ~B 'void ()' inline default trivial noexcept-unevaluated 0x5641e59953b8
    
     Layout: <CGRecordLayout
       LLVMType:%class.B = type { i32 }
       NonVirtualBaseLLVMType:%class.B = type { i32 }
       IsZeroInitializable:1
       BitFields:[
     ]>
    
     *** Dumping IRgen Record Layout
     Record: CXXRecordDecl 0x5641e59660d8 <main.cpp:8:1, line:16:1> line:8:7 referenced class A definition
     |-DefinitionData pass_in_registers standard_layout trivially_copyable has_user_declared_ctor can_const_default_init
     | |-DefaultConstructor exists non_trivial user_provided
     | |-CopyConstructor simple trivial has_const_param implicit_has_const_param
     | |-MoveConstructor exists simple trivial
     | |-CopyAssignment trivial has_const_param needs_implicit implicit_has_const_param
     | |-MoveAssignment exists simple trivial needs_implicit
     | `-Destructor simple irrelevant trivial needs_implicit
     |-CXXRecordDecl 0x5641e59661f8 <col:1, col:7> col:7 implicit referenced class A
     |-AccessSpecDecl 0x5641e5966288 <line:9:2, col:9> col:2 private
     |-FieldDecl 0x5641e59662c0 <line:10:3, col:5> col:5 b 'B'
     |-FieldDecl 0x5641e5966328 <line:11:3, col:7> col:7 referenced a 'int'
     |-AccessSpecDecl 0x5641e5966378 <line:12:2, col:8> col:2 public
     |-CXXConstructorDecl 0x5641e5966400 <line:13:3, line:15:3> line:13:3 used A 'void ()'
     | |-CXXCtorInitializer Field 0x5641e59662c0 'b' 'B'
     | | `-CXXConstructExpr 0x5641e5966818 <col:3> 'B' 'void ()'
     | `-CompoundStmt 0x5641e59668f0 <col:7, line:15:3>
     |   `-BinaryOperator 0x5641e59668d0 <line:14:5, col:9> 'int' lvalue '='
     |     |-MemberExpr 0x5641e5966880 <col:5> 'int' lvalue ->a 0x5641e5966328
     |     | `-CXXThisExpr 0x5641e5966870 <col:5> 'A *' implicit this
     |     `-IntegerLiteral 0x5641e59668b0 <col:9> 'int' 0
     |-CXXConstructorDecl 0x5641e5966b18 <line:8:7> col:7 implicit constexpr A 'void (const A &)' inline default trivial noexcept-unevaluated 0x5641e5966b18
     | `-ParmVarDecl 0x5641e5995348 <col:7> col:7 'const A &'
     `-CXXConstructorDecl 0x5641e59954c8 <col:7> col:7 implicit constexpr A 'void (A &&)' inline default trivial noexcept-unevaluated 0x5641e59954c8
       `-ParmVarDecl 0x5641e59955d8 <col:7> col:7 'A &&'
    
     Layout: <CGRecordLayout
       LLVMType:%class.A = type { %class.B, i32 }
       NonVirtualBaseLLVMType:%class.A = type { %class.B, i32 }
       IsZeroInitializable:1
       BitFields:[
     ]>

    |-CXXConstructorDecl 0x5641e5966400 <line:13:3, line:15:3> line:13:3 used A ‘void ()‘
    | |-CXXCtorInitializer Field 0x5641e59662c0 ‘b‘ ‘B‘
    | | `-CXXConstructExpr 0x5641e5966818 ‘B‘ ‘void ()‘

    可以发现,在我们定义的default constructor中,也调用了class B的default constructor,且这个调用动作发生在 a = 0;这条语句之前,然而我们并没有写呀,这便是编译器自动安插的调用

    也就是说,在这种情况下,编译器会扩充已经存在constructor,在其中安插一些代码,使得在该constgructor中所有代码执行之前,先调用必要的default constructor

// 未完,待续...

《深度探索C++对象模型》之《构造函数语义学》章节探索与理解

标签:reference   declare   详细   重点   enc   dump   strong   binary   tor   

原文地址:https://www.cnblogs.com/xhb19960928/p/11688972.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!