std::raw_storage_iterator - cppreference.com
来自cppreference.com
| 在标头 |
||
template< class OutputIt, class T > class raw_storage_iterator : public std::iterator<std::output_iterator_tag, void, void, void, void>; |
(C++17 前) | |
template< class OutputIt, class T > class raw_storage_iterator; |
(C++17 起) (C++17 弃用) (C++20 移除) |
|
输出迭代器 std::raw_storage_iterator 使得标准算法能存储结果于未初始化内存。凡在算法向解引用后的迭代器写入 T 类型的对象时,对象就被复制构造到该迭代器所指向的未初始化存储中的位置。模板形参 OutputIt 是任何满足老式输出迭代器 (LegacyOutputIterator) 要求的类型,并拥有定义为返回对象的 operator*,operator& 对该对象返回 T* 类型值。通常,以类型 T* 为 OutputIt。
类型要求
成员函数
成员类型
| 成员类型 | 定义 | ||||
iterator_category
|
std::output_iterator_tag
| ||||
value_type
|
void
| ||||
difference_type
|
| ||||
pointer
|
void
| ||||
reference
|
void
|
|
要求通过从 |
(C++17 前) |
注解
std::raw_storage_iterator 已被弃用,主要因为其行为并非异常安全。与 std::uninitialized_copy 不同,它并不会安全地处理诸如 std::copy 等操作中的异常,这可能会由于缺乏对成功构造对象数量的跟踪并在出现异常时对它们进行恰当的析构而导致资源泄露。
示例
#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); }
输出: