Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::start_lifetime_as,std::start_lifetime_as_array

      From cppreference.com
      <cpp‎ |memory
       
       
      Memory management library
      (exposition only*)
      Allocators
      Uninitialized memory algorithms
      Constrained uninitialized memory algorithms
      Memory resources
      Explicit lifetime management
      start_lifetime_as
      (C++23)
      start_lifetime_as_array
      (C++23)
      Uninitialized storage(until C++20)
      (until C++20*)
      (until C++20*)
      Garbage collector support(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
      (C++11)(until C++23)
       
      Defined in header<memory>
      std::start_lifetime_as
      template<class T>
      T* start_lifetime_as(void* p)noexcept;
      (1)(since C++23)
      template<class T>
      const T* start_lifetime_as(constvoid* p)noexcept;
      (2)(since C++23)
      template<class T>
      volatile T* start_lifetime_as(volatilevoid* p)noexcept;
      (3)(since C++23)
      template<class T>
      constvolatile T* start_lifetime_as(constvolatilevoid* p)noexcept;
      (4)(since C++23)
      std::start_lifetime_as_array
      template<class T>
      T* start_lifetime_as_array(void* p,std::size_t n)noexcept;
      (5)(since C++23)
      template<class T>

      const T* start_lifetime_as_array(constvoid* p,

                                       std::size_t n)noexcept;
      (6)(since C++23)
      template<class T>

      volatile T* start_lifetime_as_array(volatilevoid* p,

                                           std::size_t n)noexcept;
      (7)(since C++23)
      template<class T>

      constvolatile T* start_lifetime_as_array(constvolatilevoid* p,

                                                 std::size_t n)noexcept;
      (8)(since C++23)
      1-4)Implicitly creates a complete object of typeT (whose address isp) and objects nested within it. The value of each created objectobj ofTriviallyCopyable typeU is determined in the same manner as for a call tostd::bit_cast<U>(E) except that the storage is not actually accessed, whereE is the lvalue of typeU denotingobj. Otherwise, the values of such created objects are unspecified.
      • [p(char*)p+ sizeof(T)) does not denote a region of allocated storage that is a subset of the region of storage reachable throughp, or
      • the region is not suitably aligned for theT.
      • Note that the unspecified value can be indeterminate.
      5-8)Implicitly creates an array with element typeT and lengthn. To be precise, ifn>0 istrue, it is equivalent tostd::start_lifetime_as<U>(p) whereU is the type "array ofnTs". Otherwise, the function has no effects.
      • T shall be acomplete type. Otherwise, the program is ill-formed.
      • The behavior is undefined if:
      • Non-nullp is not suitably aligned for an array ofT, or
      • n<=std::size_t(-1)/ sizeof(T) isfalse, or
      • n>0 and[(char*)p(char*)p+(n* sizeof(T))) does not denote a region of allocated storage that is a subset of the region of storage reachable throughp.

      Contents

      [edit]Parameters

      p - the address of the region consisting objects
      n - the number of the element of the array to be created

      [edit]Return value

      1-4) A pointer to the complete object as described above.
      5-8) A pointer to the first element of the created array, if any; otherwise, a pointer that compares equal top.

      [edit]Notes

      new(void_ptr)unsignedchar[size] ornew(void_ptr)std::byte[size] works as an untyped version ofstd::start_lifetime_as, but it does not keep the object representation.

      std::start_lifetime_as handles non-array types as well as arrays of known bound, whilestd::start_lifetime_as_array handles arrays of unknown bound.

      Feature-test macroValueStdFeature
      __cpp_lib_start_lifetime_as202207L(C++23)Explicit lifetime management

      [edit]Example

      Run this code
      #include <complex>#include <iostream>#include <memory> int main(){    alignas(std::complex<float>)unsignedchar network_data[sizeof(std::complex<float>)]{0xcd,0xcc,0xcc,0x3d,0xcd,0xcc,0x4c,0x3e}; //  auto d = *reinterpret_cast<std::complex<float>*>(network_data);//  std::cout << d << '\n'; // UB: network_data does not point to a complex<float> //  auto d1 = *std::launder(reinterpret_cast<std::complex<float>*>(network_data));//  std::cout << d1 << '\n'; // UB: implicitly created objects have dynamic storage//                                  duration and have indeterminate value initially,//                                  even when an array which provides storage for//                                  them has determinate bytes.//                                  See also CWG2721. auto d2=*std::start_lifetime_as<std::complex<float>>(network_data);std::cout<< d2<<'\n';// OK}

      Possible output:

      (0.1,0.2)

      [edit]References

      • C++23 standard (ISO/IEC 14882:2024):
      • 20.2.6 Explicit lifetime management [obj.lifetime]

      [edit]See also

      (C++20)
      reinterpret the object representation of one type as that of another
      (function template)[edit]
      converts aspan into a view of its underlying bytes
      (function template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/start_lifetime_as&oldid=179469"

      [8]ページ先頭

      ©2009-2025 Movatter.jp