Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

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

      From cppreference.com
      <cpp‎ |language‎ |attributes
       
       
      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
       
      Declarations
       
      Attributes
      (C++23)
      (C++11)(until C++26)
      (C++14)
      (C++20)
      (C++17)
      (C++11)
      no_unique_address
      (C++20)
      (C++20)
       

      Allows this data member to be overlapped with other non-static data members or base class subobjects of its class.

      Contents

      [edit]Syntax

      [[no_unique_address]]

      [edit]Explanation

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

      Makes this member subobjectpotentially-overlapping, i.e., allows this member to be overlapped with other non-static data members or base class subobjects of its class. This means that if the member has an empty class type (e.g. stateless allocator), the compiler may optimize it to occupy no space, just like if it were anempty base. If the member is not empty, any tail padding in it may be also reused to store other data members.

      [edit]Notes

      [[no_unique_address]] is ignored by MSVC even in C++20 mode; instead,[[msvc::no_unique_address]] is provided.

      [edit]Example

      Run this code
      #include <boost/type_index.hpp>#include <iostream> struct Empty{};// The size of any object of empty class type is at least 1static_assert(sizeof(Empty)>=1); struct X{int i;    Empty e;// At least one more byte is needed to give ‘e’ a unique address};static_assert(sizeof(X)>= sizeof(int)+1); struct Y{int i;[[no_unique_address]] Empty e;// Empty member optimized out};static_assert(sizeof(Y)>= sizeof(int)); struct Z{char c;// 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’.[[no_unique_address]] Empty e1, e2;};static_assert(sizeof(Z)>=2); struct W{char c[2];// e1 and e2 cannot have the same address, but one of// them can share with c[0] and the other with 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>();}

      Possible output:

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

      [edit]References

      • C++23 standard (ISO/IEC 14882:2024):
      • 9.12.11 No unique address attribute [dcl.attr.nouniqueaddr]
      • C++20 standard (ISO/IEC 14882:2020):
      • 9.12.10 No unique address attribute [dcl.attr.nouniqueaddr]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/language/attributes/no_unique_address&oldid=181947"

      [8]ページ先頭

      ©2009-2025 Movatter.jp