Namespaces
Variants

C++ attribute: no_unique_address (since C++20)

From cppreference.net
C++ language
General topics
Flow control
Conditional execution statements
Iteration statements (loops)
Jump statements
Functions
Function declaration
Lambda function expression
inline specifier
Dynamic exception specifications ( until C++17* )
noexcept specifier (C++11)
Exceptions
Namespaces
Types
Specifiers
constexpr (C++11)
consteval (C++20)
constinit (C++20)
Storage duration specifiers
Initialization
Expressions
Alternative representations
Literals
Boolean - Integer - Floating-point
Character - String - nullptr (C++11)
User-defined (C++11)
Utilities
Attributes (C++11)
Types
typedef declaration
Type alias declaration (C++11)
Casts
Memory allocation
Classes
Class-specific function properties
Special member functions
Templates
Miscellaneous
Attributes
(C++23)
(C++11) (until C++26)
(C++14)
(C++20)
(C++17)
(C++11)
no_unique_address
(C++20)
(C++20)

このデータメンバーが、そのクラスの他の非静的データメンバーまたは基底クラスの部分オブジェクトとオーバーラップすることを許可します。

目次

翻訳のポイント: - 「Contents」→「目次」に翻訳 - HTMLタグ、属性、クラス名は一切変更せず保持 - リンク先のアンカー(#Syntaxなど)はC++関連の用語なので翻訳せず保持 - 番号部分は数値なので変更なし - セクションタイトル(Syntax, Explanationなど)はC++関連の専門用語として翻訳せず保持 - 元の書式と構造を完全に維持

構文

[ [ no_unique_address ] ]

説明

ビットフィールドではない非静的データメンバーの宣言において、宣言されている名前に適用されます。

このメンバー部分オブジェクトを potentially-overlapping にします。つまり、このメンバーがそのクラスの他の非静的データメンバーや基底クラスの部分オブジェクトと重複することを許可します。これは、メンバーが空のクラス型(例:ステートレスアロケーター)を持つ場合、コンパイラが empty base と同様に、スペースを占有しないように最適化する可能性があることを意味します。メンバーが空でない場合、その末尾のパディングも他のデータメンバーを格納するために再利用される可能性があります。

注記

[ [ no_unique_address ] ] はMSVCではC++20モードでも無視されます。代わりに、 [ [ msvc :: no_unique_address ] ] が提供されています。

#include <boost/type_index.hpp>
#include <iostream>
struct Empty {}; // 空クラス型のオブジェクトのサイズは少なくとも1
static_assert(sizeof(Empty) >= 1);
struct X
{
    int i;
    Empty e; // 'e'に一意のアドレスを与えるため、少なくとも1バイト追加で必要
};
static_assert(sizeof(X) >= sizeof(int) + 1);
struct Y
{
    int i;
    [[no_unique_address]] Empty e; // 空メンバが最適化されて除外
};
static_assert(sizeof(Y) >= sizeof(int));
struct Z
{
    char c;
    // e1とe2は同じ型を持つため、[[no_unique_address]]が指定されていても同じアドレスを共有できない
    // ただし、どちらも'c'とアドレスを共有できる
    [[no_unique_address]] Empty e1, e2;
};
static_assert(sizeof(Z) >= 2);
struct W
{
    char c[2];
    // e1とe2は同じアドレスを持つことはできないが、
    // 一方はc[0]と、もう一方はc[1]とアドレスを共有できる
    [[no_unique_address]] Empty e1, e2;
};
static_assert(sizeof(W) >= 2);
template <typename T>
void print_size_of()
{
    using boost::typeindex::type_id;
    std::cout << "sizeof(" << type_id<T>() << ") == " << sizeof(T) << '\n';
}
int main()
{
    print_size_of<Empty>();
    print_size_of<int>();
    print_size_of<X>();
    print_size_of<Y>();
    print_size_of<Z>();
    print_size_of<W>();
}

出力例:

sizeof(Empty) == 1
sizeof(int) == 4
sizeof(X) == 8
sizeof(Y) == 4
sizeof(Z) == 2
sizeof(W) == 3

参考文献

  • C++23標準 (ISO/IEC 14882:2024):
  • 9.12.11 一意でないアドレス属性 [dcl.attr.nouniqueaddr]
  • C++20規格 (ISO/IEC 14882:2020):
  • 9.12.10 No unique address属性 [dcl.attr.nouniqueaddr]