4
一段代碼勝過千言萬語。爲什麼這個結構不是標準佈局?
#include <iostream>
#include <type_traits>
using namespace std;
struct A
{
int a;
};
struct B : A
{
int b;
};
int main()
{
cout << is_standard_layout<B>::value << endl; // output false! WHY?
return 0;
}
其中有'B b; cout <<(reinterpret_cast(&b)==(&b.b))<< endl;'輸出false。 –
@PeteKirkham:這與此有關嗎? '&b ==&b.a'應該在這裏輸出'true'。 –
@MatthieuM .:是的,我相信皮特的評論是相關的。如果「B」是標準佈局,他的表達式將會輸出真值。 – Mat