Namespaces
Variants

std:: 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이 아닌 경우, 프로그램은 형식이 잘못되었습니다.

목차

매개변수

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

반환값

location

예제

#include <bit>
#include <memory>
class S
{
    int x_;
    float y_;
    double z_;
public:
    constexpr S(int x, float y, double z) : x_{x}, y_{y}, z_{z} {}
    [[nodiscard("no side-effects!")]]
    constexpr bool operator==(const S&) const noexcept = default;
};
consteval bool test()
{
    alignas(S) unsigned char storage[sizeof(S)]{};
    S uninitialized = std::bit_cast<S>(storage);
    std::destroy_at(&uninitialized);
    S* ptr = std::construct_at(std::addressof(uninitialized), 42, 2.71f, 3.14);
    const bool res{*ptr == S{42, 2.71f, 3.14}};
    std::destroy_at(ptr);
    return res;
}
static_assert(test());
int main() {}

결함 보고서

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

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

참고 항목

초기화되지 않은 저장 공간을 할당합니다
( std::allocator<T> 의 public 멤버 함수)
[static]
할당된 저장 공간에 객체를 생성합니다
(함수 템플릿)
(C++17)
주어진 주소에서 객체를 파괴합니다
(함수 템플릿)
주어진 주소에 객체를 생성합니다
(알고리즘 함수 객체)