美圖欣賞 | 設為首頁 | 加入收藏 | 網站地圖

當前位置:電腦中國 > 編程 > C++ >

《深度探索C++對象模型》之《構造函數語義學》—…

2019-10-18 14:48|來源:未知 |作者:dnzg |點擊:

構造函數語義學——Default Constructor 篇

這一章原書主要分析了:編譯器關于對象構造過程的干涉,即在對象構造這個過程中,編譯器到底在背后做了什么

這一章的重點在于 default constructor 和 copy constructor 的相關知識

Default Constructor 詳解

一些 C++的書籍中告訴我們:如果不寫任何構造函數,那么編譯器會為我們生成一個默認構造函數(default constructor)

那么,針對這個觀點,有兩個問題:

  • 這句話是正確的嗎?
  • 如果這句話是正確的,那么編譯器為我們生成的 default constructor 里有什么動作嗎?

1 這句話是正確的嗎?

這句話大體是正確的,但是 cppreference 中有更詳細的說明:

若不對類類型(struct、class 或 union)提供任何用戶聲明的構造函數,則編譯器將始終聲明一個作為其類的 inline public 成員的默認構造函數。
當存在用戶聲明的構造函數時,用戶仍可以關鍵詞 default 強制編譯器自動生成原本隱式聲明的默認構造函數。

上面這段話告訴我們:只有在沒有任何用戶聲明的構造函數的情況下,編譯器才會聲明一個 default constructor。換言之,只要有任何用戶聲明的構造函數(不一定要是 default constructor),哪怕該構造函數不是 default constructor,那么編譯器就不會為我們生成 default constructor(除了強制編譯器生成)

隱式定義的默認構造函數
若隱式聲明的默認構造函數未被定義為棄置的,則當其被 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 <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>

    這一段則說明了在 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 <col:3> 'B' 'void ()'

    可以發現,在我們定義的 default constructor 中,也調用了 class B 的 default constructor,且這個調用動作發生在 a = 0;這條語句之前,然而我們并沒有寫呀,這便是編譯器自動安插的調用

    也就是說,在這種情況下,編譯器會擴充已經存在 constructor,在其中安插一些代碼,使得在該 constgructor 中所有代碼執行之前,先調用必要的 default constructor

    如果 class A 提供了其他的 constructors,但是沒有提供 default constructor,那么編譯器還會為我們生成 default consturctor 嗎?

    這個之前已經說過了,答案是不會。依然用事實證明:

    class B {
     private:
      int b;
     public:
      B() {}
    };
    
    class A {
     private:
      B b;
      int a;
     public:
      A(int _a) : a(_a) {}
    };
    
    int main() {
      A a(1);
    }
    
    *** 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 0x55df47367d20 <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 0x55df47367e38 <col:1, col:7> col:7 implicit referenced class B
    |-AccessSpecDecl 0x55df47367ec8 <line:2:2, col:9> col:2 private
    |-FieldDecl 0x55df47367f08 <line:3:3, col:7> col:7 b 'int'
    |-AccessSpecDecl 0x55df47367f58 <line:4:2, col:8> col:2 public
    |-CXXConstructorDecl 0x55df47368008 <line:5:3, col:8> col:3 used B 'void ()'
    | `-CompoundStmt 0x55df473680c8 <col:7, col:8>
    |-CXXConstructorDecl 0x55df47368628 <line:1:7> col:7 implicit constexpr B 'void (const B &)' inline default trivial noexcept-unevaluated 0x55df47368628
    | `-ParmVarDecl 0x55df47368738 <col:7> col:7 'const B &'
    |-CXXConstructorDecl 0x55df473687d8 <col:7> col:7 implicit constexpr B 'void (B &&)' inline default trivial noexcept-unevaluated 0x55df473687d8
    | `-ParmVarDecl 0x55df473688e8 <col:7> col:7 'B &&'
    `-CXXDestructorDecl 0x55df473978a8 <col:7> col:7 implicit ~B 'void ()' inline default trivial noexcept-unevaluated 0x55df473978a8
    
    Layout: <CGRecordLayout
      LLVMType:%class.B = type { i32 }
      NonVirtualBaseLLVMType:%class.B = type { i32 }
      IsZeroInitializable:1
      BitFields:[
    ]>
    
    *** Dumping IRgen Record Layout
    Record: CXXRecordDecl 0x55df473680d8 <main.cpp:8:1, line:14: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
    | |-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 0x55df473681f8 <col:1, col:7> col:7 implicit referenced class A
    |-AccessSpecDecl 0x55df47368288 <line:9:2, col:9> col:2 private
    |-FieldDecl 0x55df473682c0 <line:10:3, col:5> col:5 b 'B'
    |-FieldDecl 0x55df47368328 <line:11:3, col:7> col:7 a 'int'
    |-AccessSpecDecl 0x55df47368378 <line:12:2, col:8> col:2 public
    |-CXXConstructorDecl 0x55df473684b8 <line:13:3, col:22> col:3 used A 'void (int)'
    | |-ParmVarDecl 0x55df473683b8 <col:5, col:9> col:9 used _a 'int'
    | |-CXXCtorInitializer Field 0x55df473682c0 'b' 'B'
    | | `-CXXConstructExpr 0x55df47368958 <col:3> 'B' 'void ()'
    | |-CXXCtorInitializer Field 0x55df47368328 'a' 'int'
    | | `-ImplicitCastExpr 0x55df473685c0 <col:17> 'int' <LValueToRValue>
    | |   `-DeclRefExpr 0x55df47368580 <col:17> 'int' lvalue ParmVar 0x55df473683b8 '_a' 'int'
    | `-CompoundStmt 0x55df473689b8 <col:21, col:22>
    |-CXXConstructorDecl 0x55df47397720 <line:8:7> col:7 implicit constexpr A 'void (const A &)' inline default trivial noexcept-unevaluated 0x55df47397720
    | `-ParmVarDecl 0x55df47397838 <col:7> col:7 'const A &'
    `-CXXConstructorDecl 0x55df473979b8 <col:7> col:7 implicit constexpr A 'void (A &&)' inline default trivial noexcept-unevaluated 0x55df473979b8
      `-ParmVarDecl 0x55df47397ac8 <col:7> col:7 'A &&'
    
    Layout: <CGRecordLayout
      LLVMType:%class.A = type { %class.B, i32 }
      NonVirtualBaseLLVMType:%class.A = type { %class.B, i32 }
      IsZeroInitializable:1
      BitFields:[
    ]>
    
    Record: CXXRecordDecl 0x55df473680d8 <main.cpp:8:1, line:14: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

    這幾行證明了,編譯器沒有為 class A 生成 default constructor

    |-CXXConstructorDecl 0x55df473684b8 <line:13:3, col:22> col:3 used A 'void (int)'
    | |-ParmVarDecl 0x55df473683b8 <col:5, col:9> col:9 used _a 'int'
    | |-CXXCtorInitializer Field 0x55df473682c0 'b' 'B'
    | | `-CXXConstructExpr 0x55df47368958 <col:3> 'B' 'void ()'

    這幾行證明了編譯器擴充了 A 的單參數 constructor,在里面安插了對 B 的 default consturctor 的調用

  2. 帶有 default constructor 的 Base class

    如果一個沒有任何user_provided constructors的class派生自一個帶有default constructor的base class,那么這個 dervied class 的 default constructor 會被編譯器自動合成出來且是 non-trivial 的

    一個需要注意的地方是,上述成立的一個充分不必要條件是:base class 的 default constructor 只有是 non-tirvial 時,dervied class 被合成的這個 default constructor 才是 non-trivial 的;具體原因可以回顧一下之前所說的平凡默認構造函數的所需條件

    同理,如果 derived class 提供了 defualt constructor 或者其他 constructors 的話,其結果也與第 1 點中所述情況類似,讀者可以自行實驗

  3. 帶有一個 virtual function 的 class

    如果一個沒有任何user_provided constructors的class中聲明(或繼承)了一個virtual function,那么編譯器會自動為該 class 生成一個 default constructor,且該 defalut constructor 是 non-trivial 的

    舉例證明:

    // 定義了一個virtual function
    class A {
     public:
      virtual void vfunc() {}
    };
    
    // 繼承了一個virtual function
    class B: public A {
    };

    打印相關信息:

    *** Dumping AST Record Layout
             0 | class A
             0 |   (A vtable pointer)
               | [sizeof=8, dsize=8, align=8,
               |  nvsize=8, nvalign=8]
    
    *** Dumping AST Record Layout
             0 | class B
             0 |   class A (primary base)
             0 |     (A vtable pointer)
               | [sizeof=8, dsize=8, align=8,
               |  nvsize=8, nvalign=8]
    
    *** Dumping IRgen Record Layout
    Record: CXXRecordDecl 0x55d1c6dc4c50 <main.cpp:1:1, line:4:1> line:1:7 referenced class A definition
    |-DefinitionData polymorphic literal has_constexpr_non_copy_move_ctor can_const_default_init
    | |-DefaultConstructor exists non_trivial constexpr defaulted_is_constexpr
    | |-CopyConstructor simple non_trivial has_const_param implicit_has_const_param
    | |-MoveConstructor exists simple non_trivial
    | |-CopyAssignment non_trivial has_const_param implicit_has_const_param
    | |-MoveAssignment exists simple non_trivial
    | `-Destructor simple irrelevant trivial
    |-CXXRecordDecl 0x55d1c6dc4d68 <col:1, col:7> col:7 implicit class A
    |-AccessSpecDecl 0x55d1c6dc4df8 <line:2:2, col:8> col:2 public
    |-CXXMethodDecl 0x55d1c6dc4e98 <line:3:3, col:25> col:16 vfunc 'void ()' virtual
    | `-CompoundStmt 0x55d1c6dc53c0 <col:24, col:25>
    |-CXXMethodDecl 0x55d1c6dc4f98 <line:1:7> col:7 implicit constexpr operator= 'A &(const A &)' inline default noexcept-unevaluated 0x55d1c6dc4f98
    | `-ParmVarDecl 0x55d1c6dc50a8 <col:7> col:7 'const A &'
    |-CXXMethodDecl 0x55d1c6dc5148 <col:7> col:7 implicit constexpr operator= 'A &(A &&)' inline default noexcept-unevaluated 0x55d1c6dc5148
    | `-ParmVarDecl 0x55d1c6dc5258 <col:7> col:7 'A &&'
    |-CXXDestructorDecl 0x55d1c6dc52e0 <col:7> col:7 implicit ~A 'void ()' inline default trivial noexcept-unevaluated 0x55d1c6dc52e0
    |-CXXConstructorDecl 0x55d1c6df4028 <col:7> col:7 implicit used constexpr A 'void () noexcept' inline default
    | `-CompoundStmt 0x55d1c6df4738 <col:7>
    |-CXXConstructorDecl 0x55d1c6df4100 <col:7> col:7 implicit constexpr A 'void (const A &)' inline default noexcept-unevaluated 0x55d1c6df4100
    | `-ParmVarDecl 0x55d1c6df4218 <col:7> col:7 'const A &'
    `-CXXConstructorDecl 0x55d1c6df4288 <col:7> col:7 implicit constexpr A 'void (A &&)' inline default noexcept-unevaluated 0x55d1c6df4288
      `-ParmVarDecl 0x55d1c6df4398 <col:7> col:7 'A &&'
    
    Layout: <CGRecordLayout
      LLVMType:%class.A = type { i32 (...)** }
      NonVirtualBaseLLVMType:%class.A = type { i32 (...)** }
      IsZeroInitializable:1
      BitFields:[
    ]>
    
    *** Dumping IRgen Record Layout
    Record: CXXRecordDecl 0x55d1c6dc53d0 <main.cpp:7:1, line:8:1> line:7:7 referenced class B definition
    |-DefinitionData polymorphic literal has_constexpr_non_copy_move_ctor can_const_default_init
    | |-DefaultConstructor exists non_trivial constexpr defaulted_is_constexpr
    | |-CopyConstructor simple non_trivial has_const_param implicit_has_const_param
    | |-MoveConstructor exists simple non_trivial
    | |-CopyAssignment non_trivial has_const_param implicit_has_const_param
    | |-MoveAssignment exists simple non_trivial
    | `-Destructor simple irrelevant trivial
    |-public 'A'
    |-CXXRecordDecl 0x55d1c6dc5528 <col:1, col:7> col:7 implicit class B
    |-CXXMethodDecl 0x55d1c6dc5618 <col:7> col:7 implicit constexpr operator= 'B &(const B &)' inline default noexcept-unevaluated 0x55d1c6dc5618
    | `-ParmVarDecl 0x55d1c6dc5728 <col:7> col:7 'const B &'
    |-CXXMethodDecl 0x55d1c6dc57c8 <col:7> col:7 implicit constexpr operator= 'B &(B &&)' inline default noexcept-unevaluated 0x55d1c6dc57c8
    | `-ParmVarDecl 0x55d1c6dc58d8 <col:7> col:7 'B &&'
    |-CXXDestructorDecl 0x55d1c6dc5960 <col:7> col:7 implicit ~B 'void ()' inline default trivial noexcept-unevaluated 0x55d1c6dc5960
    |-CXXConstructorDecl 0x55d1c6df3f30 <col:7> col:7 implicit used constexpr B 'void () noexcept' inline default
    | |-CXXCtorInitializer 'A'
    | | `-CXXConstructExpr 0x55d1c6df4748 <col:7> 'A' 'void () noexcept'
    | `-CompoundStmt 0x55d1c6df47b0 <col:7>
    |-CXXConstructorDecl 0x55d1c6df4408 <col:7> col:7 implicit constexpr B 'void (const B &)' inline default noexcept-unevaluated 0x55d1c6df4408
    | `-ParmVarDecl 0x55d1c6df4518 <col:7> col:7 'const B &'
    `-CXXConstructorDecl 0x55d1c6df4588 <col:7> col:7 implicit constexpr B 'void (B &&)' inline default noexcept-unevaluated 0x55d1c6df4588
      `-ParmVarDecl 0x55d1c6df4698 <col:7> col:7 'B &&'
    
    Layout: <CGRecordLayout
      LLVMType:%class.B = type { %class.A }
      NonVirtualBaseLLVMType:%class.B = type { %class.A }
      IsZeroInitializable:1
      BitFields:[
    ]>
    
    Record: CXXRecordDecl 0x55d1c6dc4c50 <main.cpp:1:1, line:4:1> line:1:7 referenced class A definition
    |-DefinitionData polymorphic literal has_constexpr_non_copy_move_ctor can_const_default_init
    | |-DefaultConstructor exists non_trivial constexpr defaulted_is_constexpr
    
    Record: CXXRecordDecl 0x55d1c6dc53d0 <main.cpp:7:1, line:8:1> line:7:7 referenced class B definition
    |-DefinitionData polymorphic literal has_constexpr_non_copy_move_ctor can_const_default_init
    | |-DefaultConstructor exists non_trivial constexpr defaulted_is_constexpr

    這幾行證明了之前的所述的觀點

    與之前的情況不同的時,這種情況下所生成的 default constructor,不僅需要調用必須的基類的 default constructor,還需要完成設定 vptr 的動作,才能滿足虛機制的運行

    與之前相同的是,如果提供了任何用戶聲明的 constructors,那么自然不會生成 default constructor,編譯器會對現有的 constrcutors 進行擴充,來完成必要的工作

    還是舉個例子吧:

    class A {
     public:
      virtual void vfunc() {}
      A(int a) : _a(a) {}
     private:
      int _a;
    };
    
    class B : public A {
     public:
      B(int a, int b) : A(a), _b(b) {}
     private:
      int _b;
    };

    打印相關信息:

    *** Dumping AST Record Layout
             0 | class A
             0 |   (A vtable pointer)
             8 |   int _a
               | [sizeof=16, dsize=12, align=8,
               |  nvsize=12, nvalign=8]
    
    *** Dumping AST Record Layout
             0 | class B
             0 |   class A (primary base)
             0 |     (A vtable pointer)
             8 |     int _a
            12 |   int _b
               | [sizeof=16, dsize=16, align=8,
               |  nvsize=16, nvalign=8]
    
    *** Dumping IRgen Record Layout
    Record: CXXRecordDecl 0x56506d274d60 <main.cpp:1:1, line:7:1> line:1:7 referenced class A definition
    |-DefinitionData polymorphic has_user_declared_ctor can_const_default_init
    | |-DefaultConstructor
    | |-CopyConstructor simple non_trivial has_const_param implicit_has_const_param
    | |-MoveConstructor exists simple non_trivial
    | |-CopyAssignment non_trivial has_const_param implicit_has_const_param
    | |-MoveAssignment exists simple non_trivial
    | `-Destructor simple irrelevant trivial
    |-CXXRecordDecl 0x56506d274e78 <col:1, col:7> col:7 implicit referenced class A
    |-AccessSpecDecl 0x56506d274f08 <line:2:2, col:8> col:2 public
    |-CXXMethodDecl 0x56506d274fa8 <line:3:3, col:25> col:16 vfunc 'void ()' virtual
    | `-CompoundStmt 0x56506d275700 <col:24, col:25>
    |-CXXConstructorDecl 0x56506d275138 <line:4:3, col:21> col:3 used A 'void (int)'
    | |-ParmVarDecl 0x56506d275060 <col:5, col:9> col:9 used a 'int'
    | |-CXXCtorInitializer Field 0x56506d275228 '_a' 'int'
    | | `-ImplicitCastExpr 0x56506d275750 <col:17> 'int' <LValueToRValue>
    | |   `-DeclRefExpr 0x56506d275710 <col:17> 'int' lvalue ParmVar 0x56506d275060 'a' 'int'
    | `-CompoundStmt 0x56506d275798 <col:20, col:21>
    |-AccessSpecDecl 0x56506d2751e8 <line:5:2, col:9> col:2 private
    |-FieldDecl 0x56506d275228 <line:6:3, col:7> col:7 _a 'int'
    |-CXXMethodDecl 0x56506d2752d8 <line:1:7> col:7 implicit operator= 'A &(const A &)' inline default noexcept-unevaluated 0x56506d2752d8
    | `-ParmVarDecl 0x56506d2753e8 <col:7> col:7 'const A &'
    |-CXXMethodDecl 0x56506d275488 <col:7> col:7 implicit operator= 'A &(A &&)' inline default noexcept-unevaluated 0x56506d275488
    | `-ParmVarDecl 0x56506d275598 <col:7> col:7 'A &&'
    |-CXXDestructorDecl 0x56506d275620 <col:7> col:7 implicit ~A 'void ()' inline default trivial noexcept-unevaluated 0x56506d275620
    |-CXXConstructorDecl 0x56506d2a4af0 <col:7> col:7 implicit constexpr A 'void (const A &)' inline default noexcept-unevaluated 0x56506d2a4af0
    | `-ParmVarDecl 0x56506d2a4c08 <col:7> col:7 'const A &'
    `-CXXConstructorDecl 0x56506d2a4c78 <col:7> col:7 implicit constexpr A 'void (A &&)' inline default noexcept-unevaluated 0x56506d2a4c78
      `-ParmVarDecl 0x56506d2a4d88 <col:7> col:7 'A &&'
    
    Layout: <CGRecordLayout
      LLVMType:%class.A = type <{ i32 (...)**, i32, [4 x i8] }>
      NonVirtualBaseLLVMType:%class.A.base = type <{ i32 (...)**, i32 }>
      IsZeroInitializable:1
      BitFields:[
    ]>
    
    *** Dumping IRgen Record Layout
    Record: CXXRecordDecl 0x56506d2757a8 <main.cpp:9:1, line:14:1> line:9:7 referenced class B definition
    |-DefinitionData polymorphic has_user_declared_ctor can_const_default_init
    | |-DefaultConstructor
    | |-CopyConstructor simple non_trivial has_const_param implicit_has_const_param
    | |-MoveConstructor exists simple non_trivial
    | |-CopyAssignment non_trivial has_const_param implicit_has_const_param
    | |-MoveAssignment exists simple non_trivial
    | `-Destructor simple irrelevant trivial
    |-public 'A'
    |-CXXRecordDecl 0x56506d275908 <col:1, col:7> col:7 implicit referenced class B
    |-AccessSpecDecl 0x56506d275998 <line:10:2, col:8> col:2 public
    |-CXXConstructorDecl 0x56506d275b68 <line:11:3, col:34> col:3 used B 'void (int, int)'
    | |-ParmVarDecl 0x56506d2759d8 <col:5, col:9> col:9 used a 'int'
    | |-ParmVarDecl 0x56506d275a58 <col:12, col:16> col:16 used b 'int'
    | |-CXXCtorInitializer 'A'
    | | `-CXXConstructExpr 0x56506d2a4e10 <col:21, col:24> 'A' 'void (int)'
    | |   `-ImplicitCastExpr 0x56506d2a4df8 <col:23> 'int' <LValueToRValue>
    | |     `-DeclRefExpr 0x56506d2a4aa0 <col:23> 'int' lvalue ParmVar 0x56506d2759d8 'a' 'int'
    | |-CXXCtorInitializer Field 0x56506d2a45d0 '_b' 'int'
    | | `-ImplicitCastExpr 0x56506d2a4ea8 <col:30> 'int' <LValueToRValue>
    | |   `-DeclRefExpr 0x56506d2a4e68 <col:30> 'int' lvalue ParmVar 0x56506d275a58 'b' 'int'
    | `-CompoundStmt 0x56506d2a4ef8 <col:33, col:34>
    |-AccessSpecDecl 0x56506d275c20 <line:12:2, col:9> col:2 private
    |-FieldDecl 0x56506d2a45d0 <line:13:3, col:7> col:7 _b 'int'
    |-CXXMethodDecl 0x56506d2a4678 <line:9:7> col:7 implicit operator= 'B &(const B &)' inline default noexcept-unevaluated 0x56506d2a4678
    | `-ParmVarDecl 0x56506d2a4788 <col:7> col:7 'const B &'
    |-CXXMethodDecl 0x56506d2a4828 <col:7> col:7 implicit operator= 'B &(B &&)' inline default noexcept-unevaluated 0x56506d2a4828
    | `-ParmVarDecl 0x56506d2a4938 <col:7> col:7 'B &&'
    |-CXXDestructorDecl 0x56506d2a49c0 <col:7> col:7 implicit ~B 'void ()' inline default trivial noexcept-unevaluated 0x56506d2a49c0
    |-CXXConstructorDecl 0x56506d2a5150 <col:7> col:7 implicit constexpr B 'void (const B &)' inline default noexcept-unevaluated 0x56506d2a5150
    | `-ParmVarDecl 0x56506d2a5268 <col:7> col:7 'const B &'
    `-CXXConstructorDecl 0x56506d2a52d8 <col:7> col:7 implicit constexpr B 'void (B &&)' inline default noexcept-unevaluated 0x56506d2a52d8
      `-ParmVarDecl 0x56506d2a53e8 <col:7> col:7 'B &&'
    
    Layout: <CGRecordLayout
      LLVMType:%class.B = type { %class.A.base, i32 }
      NonVirtualBaseLLVMType:%class.B = type { %class.A.base, i32 }
      IsZeroInitializable:1
      BitFields:[
    ]>

    讀者想必應該能夠自己尋找答案了

  4. 帶有一個 virtual base class 的 class

    為了實現 virtual base class 的語義和功能,在 derived class 中必然需要進行一些額外的工作,這些工作是編譯器來完成的

    如果存在任何 user_provided constructors,那么這些工作會被安插進入這些 user_provided constructors 中,如果沒有 user_provided constructors,那么編譯器會合成 non-trivial default constructor 來完成必要的工作(比如調用基類的 default constructor、設定虛機制所必須的相關信息之類)

    最后再舉個例子吧:

    class A {
     private:
      int _a;
    };
    
    class B: public virtual A {
     private:
      int _b;
    };

    打印相關信息:

    *** Dumping AST Record Layout
             0 | class A
             0 |   int _a
               | [sizeof=4, dsize=4, align=4,
               |  nvsize=4, nvalign=4]
    
    *** Dumping AST Record Layout
             0 | class B
             0 |   (B vtable pointer)
             8 |   int _b
            12 |   class A (virtual base)
            12 |     int _a
               | [sizeof=16, dsize=16, align=8,
               |  nvsize=12, nvalign=8]
    
    *** Dumping IRgen Record Layout
    Record: CXXRecordDecl 0x557ad6558c50 <main.cpp:1:1, line:4:1> line:1:7 referenced class A definition
    |-DefinitionData pass_in_registers standard_layout trivially_copyable trivial literal
    | |-DefaultConstructor exists trivial
    | |-CopyConstructor simple trivial has_const_param implicit_has_const_param
    | |-MoveConstructor exists simple trivial
    | |-CopyAssignment trivial has_const_param implicit_has_const_param
    | |-MoveAssignment exists simple trivial
    | `-Destructor simple irrelevant trivial
    |-CXXRecordDecl 0x557ad6558d68 <col:1, col:7> col:7 implicit class A
    |-AccessSpecDecl 0x557ad6558df8 <line:2:2, col:9> col:2 private
    |-FieldDecl 0x557ad6558e38 <line:3:3, col:7> col:7 _a 'int'
    |-CXXMethodDecl 0x557ad6559368 <line:1:7> col:7 implicit constexpr operator= 'A &(const A &)' inline default trivial noexcept-unevaluated 0x557ad6559368
    | `-ParmVarDecl 0x557ad6559478 <col:7> col:7 'const A &'
    |-CXXMethodDecl 0x557ad6559518 <col:7> col:7 implicit constexpr operator= 'A &(A &&)' inline default trivial noexcept-unevaluated 0x557ad6559518
    | `-ParmVarDecl 0x557ad6559628 <col:7> col:7 'A &&'
    |-CXXDestructorDecl 0x557ad6559970 <col:7> col:7 implicit ~A 'void ()' inline default trivial noexcept-unevaluated 0x557ad6559970
    |-CXXConstructorDecl 0x557ad6586a08 <col:7> col:7 implicit used A 'void () noexcept' inline default trivial
    | `-CompoundStmt 0x557ad6587118 <col:7>
    |-CXXConstructorDecl 0x557ad6586c68 <col:7> col:7 implicit constexpr A 'void (const A &)' inline default trivial noexcept-unevaluated 0x557ad6586c68
    | `-ParmVarDecl 0x557ad6586d78 <col:7> col:7 'const A &'
    `-CXXConstructorDecl 0x557ad6586de8 <col:7> col:7 implicit constexpr A 'void (A &&)' inline default trivial noexcept-unevaluated 0x557ad6586de8
      `-ParmVarDecl 0x557ad6586ef8 <col:7> col:7 'A &&'
    
    Layout: <CGRecordLayout
      LLVMType:%class.A = type { i32 }
      NonVirtualBaseLLVMType:%class.A = type { i32 }
      IsZeroInitializable:1
      BitFields:[
    ]>
    
    *** Dumping IRgen Record Layout
    Record: CXXRecordDecl 0x557ad6558ea0 <main.cpp:6:1, line:9:1> line:6:7 referenced class B definition
    |-DefinitionData
    | |-DefaultConstructor exists non_trivial
    | |-CopyConstructor simple non_trivial has_const_param implicit_has_const_param
    | |-MoveConstructor exists simple non_trivial
    | |-CopyAssignment non_trivial has_const_param implicit_has_const_param
    | |-MoveAssignment exists simple non_trivial
    | `-Destructor simple irrelevant trivial
    |-virtual public 'A'
    |-CXXRecordDecl 0x557ad6559010 <col:1, col:7> col:7 implicit class B
    |-AccessSpecDecl 0x557ad65590a0 <line:7:2, col:9> col:2 private
    |-FieldDecl 0x557ad65590e0 <line:8:3, col:7> col:7 _b 'int'
    |-CXXMethodDecl 0x557ad6559188 <line:6:7> col:7 implicit operator= 'B &(const B &)' inline default noexcept-unevaluated 0x557ad6559188
    | `-ParmVarDecl 0x557ad6559298 <col:7> col:7 'const B &'
    |-CXXMethodDecl 0x557ad65596c8 <col:7> col:7 implicit operator= 'B &(B &&)' inline default noexcept-unevaluated 0x557ad65596c8
    | `-ParmVarDecl 0x557ad65597d8 <col:7> col:7 'B &&'
    |-CXXDestructorDecl 0x557ad6559860 <col:7> col:7 implicit ~B 'void ()' inline default trivial noexcept-unevaluated 0x557ad6559860
    |-CXXConstructorDecl 0x557ad6586910 <col:7> col:7 implicit used B 'void () noexcept' inline default
    | |-CXXCtorInitializer 'A'
    | | `-CXXConstructExpr 0x557ad6587128 <col:7> 'A' 'void () noexcept'
    | `-CompoundStmt 0x557ad6587190 <col:7>
    |-CXXConstructorDecl 0x557ad6586ae0 <col:7> col:7 implicit B 'void (const B &)' inline default noexcept-unevaluated 0x557ad6586ae0
    | `-ParmVarDecl 0x557ad6586bf8 <col:7> col:7 'const B &'
    `-CXXConstructorDecl 0x557ad6586f68 <col:7> col:7 implicit B 'void (B &&)' inline default noexcept-unevaluated 0x557ad6586f68
      `-ParmVarDecl 0x557ad6587078 <col:7> col:7 'B &&'
    
    Layout: <CGRecordLayout
      LLVMType:%class.B = type <{ i32 (...)**, i32, %class.A }>
      NonVirtualBaseLLVMType:%class.B.base = type <{ i32 (...)**, i32 }>
      IsZeroInitializable:1
      BitFields:[
    ]>
    
    Record: CXXRecordDecl 0x557ad6558ea0 <main.cpp:6:1, line:9:1> line:6:7 referenced class B definition
    |-DefinitionData
    | |-DefaultConstructor exists non_trivial

    可以看到 class B 存在 default constructor 且是 non-trivial 的

總結

  1. 如果沒有任何 user_provided constructor,那么編譯器會始終生成一個 default constructor(反之,如果存在任何 user_provided constrcutor,那么除非強制生成,否則編譯器是不會生成 defualt construcotor 的)
  2. 編譯器生成的 default constructor 分 trivial 和 non-trivial 兩種

C++新手一般有兩個誤區:

  • 任何 class 如果沒有定義 default constructor,編譯器就會自動合成一個
  • 編譯器合成出來的 default constructor 會為成員變量設定默認值

PS:default constructor 的大概知識就到此結束啦,本文也只是走馬觀花的介紹了 default constructor 的相關知識,更加詳細的還是請移步至 cppreference 查閱相關文檔

(責任編輯:dnzg)
安卓手机安全赚钱软件哪个好用