Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/memory/raw storage iterator"

From cppreference.com
< cpp‎ | memory
(move dcl list items to templates)
m (Add notes regarding the reason of deprecation)
 
(14 intermediate revisions by 7 users not shown)
Line 1: Line 1:
 
{{cpp/title|raw_storage_iterator}}
 
{{cpp/title|raw_storage_iterator}}
{{cpp/memory/allocator/navbar}}
+
{{cpp/memory/raw_storage_iterator/navbar}}
{{ddcl list begin}}
+
{{dcl begin}}
{{ddcl list header| memory}}
+
{{dcl header|memory}}
{{ddcl list item | |1=
+
{{dcl rev begin}}
 +
{{dcl|until=c++17|1=
 
template< class OutputIt, class T >
 
template< class OutputIt, class T >
 
class raw_storage_iterator
 
class raw_storage_iterator
 
     : public std::iterator<std::output_iterator_tag, void, void, void, void>;
 
     : public std::iterator<std::output_iterator_tag, void, void, void, void>;
 
}}
 
}}
{{ddcl list end}}
+
{{dcl|since=c++17|deprecated=c++17|removed=c++20|1=
 +
template< class OutputIt, class T >
 +
class raw_storage_iterator;
 +
}}
 +
{{dcl rev end}}
 +
{{dcl end}}
  
The output iterator {{tt|std::raw_storage_iterator}} makes it possible for standard algorithms to store results in uninitialized memory. Whenever the algorithm writes an object of type {{tt|T}} to the dereferenced iterator, the object is copy-constructed into the location in the uninitialized storage pointed to by the iterator. The template parameter {{tt|OutputIt}} is any type that meets thee requirements of {{concept|OutputIterator}} and has {{c|operator*}} defined to return an object, for which {{c|operator&}} returns an object of type {{tt|T*}}. Usually, the type {{tt|T*}} is used as {{tt|OutputIt}}.
+
The output iterator {{tt|std::raw_storage_iterator}} makes it possible for standard algorithms to store results in uninitialized memory. Whenever the algorithm writes an object of type {{tt|T}} to the dereferenced iterator, the object is copy-constructed into the location in the uninitialized storage pointed to by the iterator. The template parameter {{tt|OutputIt}} is any type that meets the requirements of {{named req|OutputIterator}} and has {{c|operator*}} defined to return an object, for which {{c|operator&}} returns an object of type {{tt|T*}}. Usually, the type {{tt|T*}} is used as {{tt|OutputIt}}.
  
 
===Type requirements===
 
===Type requirements===
{{param list begin}}
+
{{par begin}}
{{param list req concept | OutputIt | OutputIterator}}
+
{{par req named|OutputIt|OutputIterator}}
{{param list end}}
+
{{par end}}
  
 
===Member functions===
 
===Member functions===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/memory/raw_storage_iterator/dcl list constructor}}
+
{{dsc inc|cpp/memory/raw_storage_iterator/dsc constructor}}
{{dcl list template | cpp/memory/raw_storage_iterator/dcl list operator{{=}}}}
+
{{dsc inc|cpp/memory/raw_storage_iterator/dsc operator{{=}}}}
{{dcl list template | cpp/memory/raw_storage_iterator/dcl list operator*}}
+
{{dsc inc|cpp/memory/raw_storage_iterator/dsc operator*}}
{{dcl list template | cpp/memory/raw_storage_iterator/dcl list operator_arith}}
+
{{dsc inc|cpp/memory/raw_storage_iterator/dsc operator_arith}}
{{dcl list end}}
+
{{dsc inc|cpp/memory/raw_storage_iterator/dsc base}}
 +
{{dsc end}}
  
 
{{cpp/iterator/iterator/inherit|std::output_iterator_tag|void|void|void|void}}
 
{{cpp/iterator/iterator/inherit|std::output_iterator_tag|void|void|void|void}}
 +
 +
===Note===
 +
{{tt|std::raw_storage_iterator}} was deprecated primarily because of its exception-unsafe behavior. Unlike {{lc|std::uninitialized_copy}}, it doesn't handle exceptions during operations like {{lc|std::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.
  
 
===Example===
 
===Example===
 
{{example
 
{{example
|
+
|code=
| code=
+
#include <algorithm>
 
#include <iostream>
 
#include <iostream>
#include <string>
 
 
#include <memory>
 
#include <memory>
#include <algorithm>
+
#include <string>
  
 
int main()
 
int main()
 
{
 
{
 
     const std::string s[] = {"This", "is", "a", "test", "."};
 
     const std::string s[] = {"This", "is", "a", "test", "."};
     std::string* p = std::get_temporary_buffer<std::string>(5).first;
+
     std::string* p = std::allocator<std::string>().allocate(5);
 
+
 
     std::copy(std::begin(s), std::end(s),
 
     std::copy(std::begin(s), std::end(s),
 
               std::raw_storage_iterator<std::string*, std::string>(p));
 
               std::raw_storage_iterator<std::string*, std::string>(p));
 
+
     for(std::string* i = p; i!=p+5; ++i) {
+
     for (std::string* i = p; i != p + 5; ++i)
 +
    {
 
         std::cout << *i << '\n';
 
         std::cout << *i << '\n';
 
         i->~basic_string<char>();
 
         i->~basic_string<char>();
 
     }
 
     }
     std::return_temporary_buffer(p);
+
     std::allocator<std::string>().deallocate(p, 5);
 
}
 
}
| output=
+
|output=
 
This
 
This
 
is
 
is
Line 59: Line 69:
  
 
===See also===
 
===See also===
{{dcl list begin}}
+
{{dsc begin}}
{{dcl list template | cpp/memory/dcl list allocator_traits}}
+
{{dsc inc|cpp/memory/dsc allocator_traits}}
{{dcl list template | cpp/memory/dcl list scoped_allocator_adaptor}}
+
{{dsc inc|cpp/memory/dsc scoped_allocator_adaptor}}
{{dcl list template | cpp/memory/dcl list uses_allocator}}
+
{{dsc inc|cpp/memory/dsc uses_allocator}}
{{dcl list end}}
+
{{dsc end}}
  
[[de:cpp/memory/raw storage iterator]]
+
{{langlinks|de|es|fr|it|ja|pt|ru|zh}}
[[es:cpp/memory/raw storage iterator]]
+
[[fr:cpp/memory/raw storage iterator]]
+
[[it:cpp/memory/raw storage iterator]]
+
[[ja:cpp/memory/raw storage iterator]]
+
[[pt:cpp/memory/raw storage iterator]]
+
[[ru:cpp/memory/raw storage iterator]]
+
[[zh:cpp/memory/raw storage iterator]]
+

Latest revision as of 01:09, 23 October 2023

 
 
Dynamic memory management
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Allocators
Garbage collection support
(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)



Uninitialized storage
raw_storage_iterator
(until C++20*)
(until C++20*)
 
 
Defined in header <memory>
template< class OutputIt, class T >

class raw_storage_iterator

    : public std::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 iterator std::raw_storage_iterator makes it possible for standard algorithms to store results in uninitialized memory. Whenever the algorithm writes an object of type T to the dereferenced iterator, the object is copy-constructed into the location in the uninitialized storage pointed to by the iterator. The template parameter OutputIt is any type that meets the requirements of LegacyOutputIterator and has operator* defined to return an object, for which operator& returns an object of type T*. Usually, the type T* is used as OutputIt.

Contents

[edit] Type requirements

-
OutputIt must meet the requirements of LegacyOutputIterator.

[edit] Member functions

creates a new raw_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_category std::output_iterator_tag
value_type void
difference_type

void

(until C++20)

std::ptrdiff_t

(since C++20)
pointer void
reference void

Member types iterator_category, value_type, difference_type, pointer and reference are required to be obtained by inheriting from std::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. Unlike std::uninitialized_copy, it doesn't handle exceptions during operations like std::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

#include <algorithm>
#include <iostream>
#include <memory>
#include <string>
 
int main()
{
    const std::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:

This
is
a
test
.

[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]