memory_destroy_n

memory_destroy_n (std::destroy_n)

<memory> 헤더에 정의되어 있어요. 대상 범위의 처음 count개 요소를 파괴해요. C++17부터 도입됐어요.

출처: cppreference

본문

template< class ForwardIt, class Size >
ForwardIt destroy_n( ForwardIt first, Size count );   // (1) since C++17, constexpr since C++20

template< class ExecutionPolicy, class ForwardIt, class Size >
ForwardIt destroy_n( ExecutionPolicy&& policy, ForwardIt first, Size count );  // (2) since C++17
    1. 대상 범위 [first, last)의 처음 count개 요소를 다음과 같이 파괴해요.
for (; count > 0; (void) ++first, --count)
    std::destroy_at(std::addressof(*first));
return first;
    1. (1)과 같지만 policy에 따라 실행돼요.

이 오버로드는 std::is_execution_policy_v<...>true일 때만 오버로드 해석에 참여해요. (C++20까지 std::decay_t, 이후 std::remove_cvref_t)

매개변수

  • first: 파괴할 요소 범위의 시작
  • count: 파괴할 요소 수
  • policy: 사용할 실행 정책

타입 요구사항

  • ForwardIt은 LegacyForwardIterator 요구사항을 충족해야 해요.
  • Size는 정수형이거나 정수형으로 변환 가능해야 해요.

반환값

파괴한 마지막 요소 다음의 반복자(즉, count만큼 전진한 first).

std::allocator_traits<A>::destroy(alloc, p) 와 달리, 미초기화 저장 공간의 객체들을 범위 단위로 파괴할 때 써요.

T* buf = ...; int n = 3;
for (int i = 0; i < n; ++i) ::new (buf + i) T;
std::destroy_n(buf, n);   // 3개 객체 소멸자 호출

더 알아보기 (Learn more)

cppreference