0
考慮下面的例子:在編譯時評估構件的位置
struct mystruct
{
int a;
int b;
int c;
};
int main()
{
mystruct x;
std :: cout << reinterpret_cast <size_t> (&(x.b)) - reinterpret_cast <size_t> (&x) << std :: endl;
}
什麼上面所做的是使用reinterpret_cast
s至確定構件b
的位置在存儲器中的結構mystruct
內。在我的系統上(並且,我猜,在任何合理的系統上),上面的收益率爲4
。
現在,我需要做的是完全相同,但在編譯時。有沒有辦法完成這樣的事情?我需要的是一些static constexpr size_t
,在編譯時會告訴我b
的位置在mystruct
內。