How does placement new know which layout to create?

前端 未结 4 1329
礼貌的吻别
礼貌的吻别 2021-01-25 01:04
#include 
#include 
struct A { int a; };
struct B : virtual A { int b; };
struct C : virtual A  { int c; };
struct D : B,C { int d; };
in         


        
4条回答
  •  旧巷少年郎
    2021-01-25 01:36

    Where is the guarantee that the layout created by new fits into a buffer of size sizeof(B) and with offset zero

    From the the type being named in new as its argument being B. A B is being made, not an D. The type B "knows nothing" about D. The declaration of D has no influence on B; the B declaration can be put into translation units in which D doesn't appear, yet everywhere in the program there will be agreement about the size of B and is layout, regardless of whether D is also known in those places or not.

    A C++ object of type T has a size sizeof T. This means that it fits into sizeof T bytes; it cannot be the case that (sizeof T) + k bytes are required for its representation, where k > 0.

提交回复
热议问题