Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::get_temporary_buffer

      From cppreference.com
      <cpp‎ |memory
       
       
      Memory management library
      (exposition only*)
      Allocators
      Uninitialized memory algorithms
      Constrained uninitialized memory algorithms
      Memory resources
      Uninitialized storage(until C++20)
      (until C++20*)
      get_temporary_buffer
      (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>
      template<class T>

      std::pair<T*,std::ptrdiff_t>

          get_temporary_buffer(std::ptrdiff_t count);
      (until C++11)
      template<class T>

      std::pair<T*,std::ptrdiff_t>

          get_temporary_buffer(std::ptrdiff_t count)noexcept;
      (since C++11)
      (deprecated in C++17)
      (removed in C++20)

      Ifcount is negative or zero, does nothing.

      Otherwise, requests to allocate uninitialized contiguous storage forcount adjacent objects of typeT. The request is non-binding, and the implementation may instead allocate the storage for any other number of (including zero) adjacent objects of typeT.

      It is implementation-defined whetherover-aligned types are supported.

      (since C++11)

      Contents

      [edit]Parameters

      count - the desired number of objects

      [edit]Return value

      Astd::pair, the memberfirst is a pointer to the beginning of the allocated storage and the membersecond is the number of objects that fit in the storage that was actually allocated.

      Ifcount<=0 or allocated storage is not enough to store a single element of typeT, the memberfirst of the result is a null pointer and the membersecond is zero.

      [edit]Notes

      This API was originally designed with the intent of providing a more efficient implementation than the general-purposeoperator new, but no such implementation was created and the API was deprecated and removed.

      [edit]Example

      [edit]
      Run this code
      #include <algorithm>#include <iostream>#include <iterator>#include <memory>#include <string> int main(){conststd::string s[]={"string","1","test","..."};constauto p= std::get_temporary_buffer<std::string>(4);// requires that p.first is passed to return_temporary_buffer// (beware of early exit points and exceptions), or better use:std::unique_ptr<std::string,void(*)(std::string*)> on_exit(p.first,[](std::string* p){std::cout<<"returning temporary buffer...\n";std::return_temporary_buffer(p);}); std::copy(s, s+ p.second,std::raw_storage_iterator<std::string*,std::string>(p.first));// has same effect as: std::uninitialized_copy(s, s + p.second, p.first);// requires that each string in p is individually destroyed// (beware of early exit points and exceptions) std::copy(p.first, p.first+ p.second,std::ostream_iterator<std::string>{std::cout,"\n"}); std::for_each(p.first, p.first+ p.second,[](std::string& e){        e.~basic_string<char>();});// same as: std::destroy(p.first, p.first + p.second); // manually reclaim memory if unique_ptr-like technique is not used:// std::return_temporary_buffer(p.first);}

      Output:

      string1test...returning temporary buffer...

      [edit]Defect reports

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

      DRApplied toBehavior as publishedCorrect behavior
      LWG 425C++98the behavior whencount<=0 was unclearmade clear
      LWG 2072C++98it was not allowed to allocate insufficient memoryallowed

      [edit]See also

      (deprecated in C++17)(removed in C++20)
      frees uninitialized storage
      (function template)[edit]
      [static](C++23)
      allocates storage at least as large as the requested size via an allocator
      (public static member function ofstd::allocator_traits<Alloc>)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/get_temporary_buffer&oldid=156965"

      [8]ページ先頭

      ©2009-2025 Movatter.jp