std:: raw_storage_iterator
|
|
|||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||||
|
定义于头文件
<memory>
|
||
|
template
<
class
OutputIt,
class
T
>
class
raw_storage_iterator
|
(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
。
目录 |
类型要求
-
OutputIt
必须满足
LegacyOutputIterator
的要求。
|
成员函数
创建新的
raw_storage_iterator
(公开成员函数) |
|
|
在缓冲区指向位置构造对象
(公开成员函数) |
|
|
解引用迭代器
(公开成员函数) |
|
|
推进迭代器
(公开成员函数) |
|
|
(since C++17)
|
提供对包装迭代器的访问
(公开成员函数) |
成员类型
| 成员类型 | 定义 | ||||
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); }
输出:
This is a test .
参见
|
(C++11)
|
提供分配器类型的信息
(类模板) |
|
(C++11)
|
为多层容器实现多层分配器
(类模板) |
|
(C++11)
|
检查指定类型是否支持使用分配器构造
(类模板) |