Movatterモバイル変換


[0]ホーム

URL:


cppreference.com
Namespaces
Variants
    Actions

      std::raw_storage_iterator

      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)
      raw_storage_iterator
      (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>
      template<class OutputIt,class T>

      class raw_storage_iterator

         :publicstd::iterator<std::output_iterator_tag,void,void,void,void>;
      (until C++17)
      template<class OutputIt,class T>
      class raw_storage_iterator;
      (since C++17)
      (deprecated in C++17)
      (removed in C++20)

      The output iteratorstd::raw_storage_iterator makes it possible for standard algorithms to store results in uninitialized memory. Whenever the algorithm writes an object of typeT to the dereferenced iterator, the object is copy-constructed into the location in the uninitialized storage pointed to by the iterator. The template parameterOutputIt is any type that meets the requirements ofLegacyOutputIterator and hasoperator* defined to return an object, for whichoperator& returns an object of typeT*. Usually, the typeT* is used asOutputIt.

      Contents

      [edit]Type requirements

      -
      OutputIt must meet the requirements ofLegacyOutputIterator.

      [edit]Member functions

      creates a newraw_storage_iterator
      (public member function)[edit]
      constructs an object at the pointed-to location in the buffer
      (public member function)[edit]
      dereferences the iterator
      (public member function)[edit]
      advances the iterator
      (public member function)[edit]
      (since C++17)
      provides access to the wrapped iterator
      (public member function)[edit]

      [edit]Member types

      Member type Definition
      iterator_categorystd::output_iterator_tag
      value_typevoid
      difference_type

      void

      (until C++20)

      std::ptrdiff_t

      (since C++20)
      pointervoid
      referencevoid

      Member typesiterator_category,value_type,difference_type,pointer andreference are required to be obtained by inheriting fromstd::iterator<std::output_iterator_tag,void,void,void,void>.

      (until C++17)

      [edit]Note

      std::raw_storage_iterator was deprecated primarily because of its exception-unsafe behavior. Unlikestd::uninitialized_copy, it doesn't handle exceptions during operations likestd::copy safely, potentially leading to resource leaks due to a lack of tracking the number of successfully constructed objects and their proper destruction in the presence of exceptions.

      [edit]Example

      Run this code
      #include <algorithm>#include <iostream>#include <memory>#include <string> int main(){conststd::string s[]={"This","is","a","test","."};std::string* p=std::allocator<std::string>().allocate(5); std::copy(std::begin(s),std::end(s),              std::raw_storage_iterator<std::string*,std::string>(p)); for(std::string* i= p; i!= p+5;++i){std::cout<<*i<<'\n';        i->~basic_string<char>();}std::allocator<std::string>().deallocate(p,5);}

      Output:

      Thisisatest.

      [edit]See also

      provides information about allocator types
      (class template)[edit]
      implements multi-level allocator for multi-level containers
      (class template)[edit]
      checks if the specified type supports uses-allocator construction
      (class template)[edit]
      Retrieved from "https://en.cppreference.com/mwiki/index.php?title=cpp/memory/raw_storage_iterator&oldid=161197"

      [8]ページ先頭

      ©2009-2025 Movatter.jp