std::raw_storage_iterator
来自 cppreference.cn
定义于头文件 <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 (公共成员函数) | |
在缓冲区中指向的位置构造一个对象 (公共成员函数) | |
解引用迭代器 (公共成员函数) | |
推进迭代器 (公共成员函数) | |
(自 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) |
检查指定的类型是否支持 uses-allocator 构造 (类模板) |