Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | language‎ | attributes
 
 
 
 
Attributes
(C++14)
(C++17)
(C++20)(C++20)
no_unique_address
(C++20)
(C++20)(C++20)(C++20)
 

Indicates that this data member need not have an address distinct from all other non-static data members of its class.

[edit] Syntax

[[no_unique_address]]

[edit] Explanation

Applies to the name being declared in the declaration of a non­-static data member that's not a bit field.

Indicates that this data member need not have an address distinct from all other non-static data members of its class. This means that if the member has an empty type (e.g. stateless Allocator), the compiler may optimise it to occupy no space, just like if it were an empty base.

[edit] Example