Namespaces
Variants

std:: raw_storage_iterator

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
raw_storage_iterator
( until C++20* )
( until C++20* )
( until C++20* )

Garbage collector support (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
(C++11) (until C++23)
ヘッダーで定義 <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 を作成する
(public member function)
バッファ内の指し示す位置にオブジェクトを構築する
(public member function)
イテレータをデリファレンスする
(public member function)
イテレータを進める
(public member function)
(since C++17)
ラップされたイテレータへのアクセスを提供する
(public member function)

メンバー型

メンバー型 定義
iterator_category std:: output_iterator_tag
value_type void
difference_type

void

(C++20以前)

std::ptrdiff_t

(C++20以降)
pointer void
reference void

メンバ型 iterator_category , value_type , difference_type , pointer および reference は、 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構築をサポートするかどうかを検査する
(クラステンプレート)