名稱空間
變體
操作

std::raw_storage_iterator

來自 cppreference.com
< cpp‎ | 記憶體
 
 
記憶體管理庫
(僅作說明*)
未初始化記憶體演算法
(C++17)
(C++17)
(C++17)
受約束的未初始化
記憶體演算法
C 庫

分配器
記憶體資源
垃圾回收支援
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
(C++11)(直到 C++23)
未初始化儲存
raw_storage_iterator
(直到 C++20*)
(直到 C++20*)
顯式生命週期管理
 
 
定義於標頭檔案 <memory>
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

目錄

[編輯] 型別要求

-
OutputIt 必須滿足 LegacyOutputIterator 的要求。

[編輯] 成員函式

建立新的 raw_storage_iterator
(公開成員函式) [編輯]
在緩衝區中指向的位置構造一個物件
(公開成員函式) [編輯]
解引用迭代器
(公開成員函式) [編輯]
前進迭代器
(公開成員函式) [編輯]
(C++17 起)
提供對包裝迭代器的訪問
(公開成員函式) [編輯]

[編輯] 成員型別

成員型別 定義
iterator_category std::output_iterator_tag
value_type void
difference_type

void

(C++20 前)

std::ptrdiff_t

(C++20 起)
pointer void
reference void

成員型別 iterator_categoryvalue_typedifference_typepointerreference 要求透過繼承自 std::iterator<std::output_iterator_tag, void, void, void, 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
.

[編輯] 另請參閱

提供關於分配器型別的資訊
(類模板) [編輯]
為多層容器實現多層分配器
(類模板) [編輯]
檢查指定型別是否支援 uses-allocator 構造
(類模板) [編輯]