Namespaces
Variants

std::ranges:: construct_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 < class T, class ... Args >
constexpr T * construct_at ( T * location, Args && ... args ) ;
(C++20부터)

주어진 주소 location 에서 args 내 인수들로 초기화된 T 객체를 생성합니다.

다음 코드와 동등합니다: if constexpr ( std:: is_array_v < T > )
return :: new ( voidify  ( * location ) ) T [ 1 ] ( ) ;
else
return :: new ( voidify  ( * location ) ) T ( std:: forward < Args > ( args ) ... ) ;
, 단 construct_at 상수 표현식 평가에 사용될 수 있습니다 (C++26 이전) .

construct_at 이 어떤 상수 표현식 expr 의 평가 과정에서 호출될 때, location 은 반드시 std:: allocator < T > :: allocate 로 얻은 저장 공간이거나 expr 의 평가 과정에서 수명이 시작된 객체를 가리켜야 합니다.

이 오버로드는 다음의 모든 조건이 충족될 경우에만 오버로드 해결에 참여합니다:

만약 std:: is_array_v < T > true 이고 sizeof... ( Args ) 가 0이 아닌 경우, 프로그램은 ill-formed입니다.

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

목차

매개변수

location - T 객체가 생성될 초기화되지 않은 저장 공간을 가리키는 포인터
args... - 초기화에 사용되는 인수들

반환값

location

참고 사항

std::ranges::construct_at 는 인수 종속 lookup에 보이지 않는다는 점을 제외하면 std::construct_at 과 완전히 동일하게 동작합니다.

예제

#include <iostream>
#include <memory>
struct S
{
    int x;
    float y;
    double z;
    S(int x, float y, double z) : x{x}, y{y}, z{z} { std::cout << "S::S();\n"; }
    ~S() { std::cout << "S::~S();\n"; }
    void print() const
    {
        std::cout << "S { x=" << x << "; y=" << y << "; z=" << z << "; };\n";
    }
};
int main()
{
    alignas(S) unsigned char buf[sizeof(S)];
    S* ptr = std::ranges::construct_at(reinterpret_cast<S*>(buf), 42, 2.71828f, 3.1415);
    ptr->print();
    std::ranges::destroy_at(ptr);
}

출력:

S::S();
S { x=42; y=2.71828; z=3.1415; };
S::~S();

결함 보고서

다음의 동작 변경 결함 보고서들은 이전에 발표된 C++ 표준에 소급 적용되었습니다.

DR 적용 대상 게시된 동작 올바른 동작
LWG 3436 C++20 construct_at 배열 타입의 객체를 생성할 수 없었음 경계가 지정된 배열을 값 초기화할 수 있음
LWG 3870 C++20 construct_at cv 한정 타입의 객체를 생성할 수 있었음 cv 비한정 타입만 허용됨

참고 항목

주어진 주소의 객체를 파괴함
(알고리즘 함수 객체)
주어진 주소에 객체를 생성함
(함수 템플릿)