Namespaces
Variants

std::construct_at

cppreference.net에서
 
 
알고리즘 라이브러리
제약된 알고리즘 및 범위 기반 알고리즘 (C++20)
제약된 알고리즘, 예: ranges::copy, ranges::sort, ...
정렬 및 관련 연산
분할 연산
(C++11)    

정렬 연산
이진 검색 연산
(분할된 범위에서)
집합 연산 (정렬된 범위에서)
병합 연산 (정렬된 범위에서)
힙 연산
최소/최대 연산
(C++11)
(C++17)
사전식 비교 연산
순열 연산


 
 
헤더에 정의됨 <memory>
template< class T, class... Args >
constexpr T* construct_at( T* location, Args&&... args );
(C++20부터)

T 객체를 args 안에 있는 인수로 초기화하여 주어진 주소 location에서 생성합니다.


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_unbounded_array_v<T>은 false입니다.
  • ::new(std::declval<void*>()) T(std::declval<Args>()...)는 비평가 피연산자로 취급될 때 적합합니다.

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

매개변수

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 배열 타입 객체를 생성할 수 없었음 경계 배열을 값 초기화할 수 있음

참고 항목

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