Is the Empty Base Class Optimization now a mandatory optimization (at least for standard-layout classes)?

后端 未结 1 1724
青春惊慌失措
青春惊慌失措 2020-12-03 10:17

According to C++11 9.1/7 (draft n3376), a standard-layout class is a class that:

  • has no non-static data membe

相关标签:
1条回答
  • 2020-12-03 10:51

    Yes, you're correct, that was pointed out in the "PODs revisited" proposals: http://www.open-std.org/jtc1/sc22/WG21/docs/papers/2007/n2342.htm#ABI

    The Embarcadero compiler docs also state it: http://docwiki.embarcadero.com/RADStudio/en/Is_standard_layout

    Another key point is [class.mem]/16

    Two standard-layout struct (Clause 9) types are layout-compatible if they have the same number of non-static data members and corresponding non-static data members (in declaration order) have layout-compatible types (3.9).

    Note that only data members affect layout compatibility, not base classes, so these two standard layout classes are layout-compatible:

    struct empty { };
    struct stdlayout1 : empty { int i; };
    
    struct stdlayout2 { int j; };
    
    0 讨论(0)
提交回复
热议问题