std::raw_storage_iterator
來自 cppreference.com
定義於標頭檔案 <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 構造 (類模板) |