属性: no_unique_address (C++20以上)
提供: cppreference.com
< cpp | language | attributes
そのデータメンバがそのクラスの他のすべての非静的データメンバと異なるアドレスを持つ必要がないことを示します。
[編集] 構文
[[no_unique_address]]
|
|||||||||
[編集] 説明
ビットフィールドを除く非静的データメンバの宣言で宣言されている名前に適用されます。
そのデータメンバがそのクラスの他のすべての非静的データメンバと異なるアドレスを持つ必要がないことを示します。 これは、そのメンバが空の型 (例えばステートレスなアロケータ) である場合に、空の基底であるかのように、それが空間を占めないようにコンパイラが最適化できることを意味します。 メンバが空でない場合、末尾のパディングが他のデータメンバを格納するために再利用されることもあります。
[編集] 例
Run this code
#include <iostream> struct Empty {}; // empty class struct X { int i; Empty e; }; struct Y { int i; [[no_unique_address]] Empty e; }; struct Z { char c; [[no_unique_address]] Empty e1, e2; }; struct W { char c[2]; [[no_unique_address]] Empty e1, e2; }; int main() { // the size of any object of empty class type is at least 1 static_assert(sizeof(Empty) >= 1); // at least one more byte is needed to give e a unique address static_assert(sizeof(X) >= sizeof(int) + 1); // empty member optimized out std::cout << "sizeof(Y) == sizeof(int) is " << std::boolalpha << (sizeof(Y) == sizeof(int)) << '\n'; // e1 and e2 cannot share the same address because they have the // same type, even though they are marked with [[no_unique_address]]. // However, either may share address with c. static_assert(sizeof(Z) >= 2); // e1 and e2 cannot have the same address, but one of them can share with // c[0] and the other with c[1] std::cout << "sizeof(W) == 2 is " << (sizeof(W) == 2) << '\n'; }
出力例:
sizeof(Y) == sizeof(int) is true sizeof(W) == 2 is true