Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::has_unique_object_representations

      From cppreference.com
      <cpp‎ |types
       
       
      Metaprogramming library
      Type traits
      Type categories
      (C++11)
      (C++11)(DR*)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11)
      (C++11) 
      Type properties
      (C++11)
      (C++11)
      (C++14)
      (C++11)(deprecated in C++26)
      (C++11)(until C++20*)
      (C++11)(deprecated in C++20)
      (C++11)
      has_unique_object_representations
      (C++17)
      Type trait constants
      Metafunctions
      (C++17)
      Supported operations
      Relationships and property queries
      Type modifications
      Type transformations
      (C++11)(deprecated in C++23)
      (C++11)(deprecated in C++23)
      (C++11)
      (C++11)(until C++20*)(C++17)

      Compile-time rational arithmetic
      Compile-time integer sequences
       
      Defined in header<type_traits>
      template<class T>
      struct has_unique_object_representations;
      (since C++17)

      std::has_unique_object_representations is aUnaryTypeTrait.

      IfT istrivially copyable and if any two objects of typeT with the same value have the sameobject representation, provides the member constantvalue equaltrue. For any other type,value isfalse.

      For the purpose of this trait, two arrays have the same value if their elements have the same values, two non-union classes have the same value if their direct subobjects have the same value, and two unions have the same value if they have the same active member and the value of that member is the same.

      It is implementation-defined which scalar types satisfy this trait, but unsigned(until C++20) integer types that do not use padding bits are guaranteed to have unique object representations.

      Ifstd::remove_all_extents_t<T> is an incomplete type other than (possibly cv-qualified)void, the behavior is undefined.

      If the program adds specializations forstd::has_unique_object_representations orstd::has_unique_object_representations_v, the behavior is undefined.

      Contents

      [edit]Template parameters

      T - a type to check

      [edit]Helper variable template

      template<class T>

      constexprbool has_unique_object_representations_v=

          has_unique_object_representations<T>::value;
      (since C++17)
      [edit]

      Inherited fromstd::integral_constant

      Member constants

      value
      [static]
      true ifT has unique object representations,false otherwise
      (public static member constant)

      Member functions

      operator bool
      converts the object tobool, returnsvalue
      (public member function)
      operator()
      (C++14)
      returnsvalue
      (public member function)

      Member types

      Type Definition
      value_typebool
      typestd::integral_constant<bool, value>

      [edit]Notes

      This trait was introduced to make it possible to determine whether a type can be correctly hashed by hashing its object representation as a byte array.

      Feature-test macroValueStdFeature
      __cpp_lib_has_unique_object_representations201606L(C++17)std::has_unique_object_representations

      [edit]Example

      Run this code
      #include <cstdint>#include <type_traits> struct unpadded{std::uint32_t a, b;}; struct likely_padded{std::uint8_t c;std::uint16_t st;std::uint32_t i;}; int main(){// Every value of a char corresponds to exactly one object representation.    static_assert(std::has_unique_object_representations_v<char>);// For IEC 559 floats, assertion passes because the value NaN has// multiple object representations.    static_assert(!std::has_unique_object_representations_v<float>); // Should succeed in any sane implementation because unpadded// is typically not padded, and std::uint32_t cannot contain padding bits.    static_assert(std::has_unique_object_representations_v<unpadded>);// Fails in most implementations because padding bits are inserted// between the data members c and st for the purpose of aligning st to 16 bits.    static_assert(!std::has_unique_object_representations_v<likely_padded>); // Notable architectural divergence:    static_assert(std::has_unique_object_representations_v<bool>);// x86// static_assert(!std::has_unique_object_representations_v<bool>); // ARM}

      [edit]Defect reports

      The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

      DRApplied toBehavior as publishedCorrect behavior
      LWG 4113C++17T could be an array of unknown bound
      even if its element type is incomplete
      required the element
      type to be complete

      [edit]See also

      checks if a type is astandard-layout type
      (class template)[edit]
      (C++11)
      hash function object
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/types/has_unique_object_representations&oldid=180849"

      [8]ページ先頭

      ©2009-2025 Movatter.jp