Namespaces
Variants

std::ranges:: destroy_at

From cppreference.net
Memory management library
( exposition only* )
Allocators
Uninitialized memory algorithms
Constrained uninitialized memory algorithms
Memory resources
Uninitialized storage (until C++20)
( 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 < std:: destructible T >
constexpr void destroy_at ( T * p ) noexcept ;
(C++20부터)

만약 T 가 배열 타입이 아니라면, p 가 가리키는 객체의 소멸자를 호출합니다. 마치 p - > ~T ( ) 와 같이 수행됩니다. 그렇지 않으면, * p 의 요소들을 순서대로 재귀적으로 파괴합니다. 마치 std:: destroy ( std:: begin ( * p ) , std:: end ( * p ) ) 를 호출하는 것처럼 수행됩니다.

이 페이지에서 설명하는 함수형 개체들은 algorithm function objects (일반적으로 niebloids 로 알려진) 즉:

목차

매개변수

p - 파괴될 객체에 대한 포인터

가능한 구현

struct destroy_at_fn
{
    template<std::destructible T>
    constexpr void operator()(T* p) const noexcept
    {
        if constexpr (std::is_array_v<T>)
            for (auto& elem : *p)
                operator()(std::addressof(elem));
        else
            p->~T();
    }
};
inline constexpr destroy_at_fn destroy_at{};

참고 사항

destroy_at 는 파괴할 객체의 타입을 추론하여 소멸자 호출 시 명시적으로 작성하는 것을 피합니다.

destroy_at 이 일부 상수 표현식 e 의 평가 중에 호출될 때, 인수 p 는 수명이 e 의 평가 내에서 시작된 객체를 가리켜야 합니다.

예제

다음 예제는 연속된 요소 시퀀스를 파괴하기 위해 ranges::destroy_at 을 사용하는 방법을 보여줍니다.

#include <iostream>
#include <memory>
#include <new>
struct Tracer
{
    int value;
    ~Tracer() { std::cout << value << " destructed\n"; }
};
int main()
{
    alignas(Tracer) unsigned char buffer[sizeof(Tracer) * 8];
    for (int i = 0; i != 8; ++i)
        new(buffer + sizeof(Tracer) * i) Tracer{i}; // manually construct objects
    auto ptr = std::launder(reinterpret_cast<Tracer*>(buffer));
    for (int i = 0; i != 8; ++i)
        std::ranges::destroy_at(ptr + i);
}

출력:

0 destructed
1 destructed
2 destructed
3 destructed
4 destructed
5 destructed
6 destructed
7 destructed

참고 항목

객체 범위를 파괴함
(알고리즘 함수 객체)
범위 내 지정된 개수의 객체를 파괴함
(알고리즘 함수 객체)
지정된 주소에 객체를 생성함
(알고리즘 함수 객체)
(C++17)
지정된 주소의 객체를 파괴함
(함수 템플릿)