memory_raw_storage_iterator

memory_raw_storage_iterator (std::raw_storage_iterator)

<memory> 헤더에 정의되어 있어요. 표준 알고리즘이 초기화되지 않은 메모리에 결과를 저장할 수 있게 해주는 출력 반복자예요. C++17에서 deprecated, C++20에서 제거됐어요.

출처: cppreference

본문

template< class OutputIt, class T >
class raw_storage_iterator
    : public std::iterator<std::output_iterator_tag, void, void, void, void>;   // until C++17

template< class OutputIt, class T >
class raw_storage_iterator;   // since C++17, deprecated in C++17, removed in C++20

출력 반복자 std::raw_storage_iterator는 표준 알고리즘이 초기화되지 않은 메모리에 결과를 저장할 수 있게 해줘요. 알고리즘이 역참조된 반복자에 T 타입 객체를 쓰면, 그 객체가 반복자가 가리키는 초기화되지 않은 저장 공간 위치에 복사 생성돼요. 템플릿 매개변수 OutputIt은 LegacyOutputIterator 요구사항을 만족하고, operator*operator&T* 타입 객체를 반환하는 객체를 돌려주는 아무 타입이에요. 보통 T*OutputIt으로 쓰여요.

멤버 함수

  • (생성자): 새 raw_storage_iterator 생성
  • operator=: 버퍼의 가리킨 위치에 객체 구성
  • operator*: 반복자 역참조
  • operator++/operator++(int): 반복자 전진
  • base (C++17): 감싸진 반복자에 접근 제공

멤버 타입

  • iterator_category: std::output_iterator_tag
  • value_type, pointer, reference: void
  • difference_type: void(C++20 이전), std::ptrdiff_t(C++20)

주의 (Notes)

std::raw_storage_iterator는 주로 예외에 안전하지 않은 동작 때문에 deprecated됐어요. 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
.

더 알아보기 (Learn more)

cppreference