Namespaces
Variants

std::any::emplace

cppreference.net에서
 
 
유틸리티 라이브러리
일반 유틸리티
관계 연산자 (C++20에서 폐기됨)
정수 비교 함수
(C++20)(C++20)(C++20)    
(C++20)
교체 및 타입 연산
(C++14)
(C++11)
(C++11)
(C++11)
(C++17)
공통 어휘 타입
(C++11)
(C++17)
(C++17)
(C++17)
(C++11)
(C++17)
(C++23)



 
 
template< class ValueType, class... Args >
std::decay_t<ValueType>& emplace( Args&&... args );
(1) (C++17부터)
template< class ValueType, class U, class... Args >
std::decay_t<ValueType>& emplace( std::initializer_list<U> il, Args&&... args );
(2) (C++17부터)

인수들로부터 생성된 타입 std::decay_t<ValueType>의 객체로 포함된 객체를 변경합니다.

먼저 reset을 사용하여 현재 포함된 객체(있는 경우)를 소멸시킨 후, 다음을 수행합니다:

1) 타입 std::decay_t<ValueType>의 객체를 직접 비목록 초기화하여 std::forward<Args>(args)...로부터 생성된 객체를 포함된 객체로 생성합니다.
  • 이 오버로드는 std::is_constructible_v<std::decay_t<ValueType>, Args...>와 std::is_copy_constructible_v<std::decay_t<ValueType>>이 모두 true인 경우에만 오버로드 해결에 참여합니다.
2) 타입 std::decay_t<ValueType>의 객체를 직접 비목록 초기화하여 il, std::forward<Args>(args)...로부터 생성된 객체를 포함된 객체로 생성합니다.
  • 이 오버로드는 std::is_constructible_v<std::decay_t<ValueType>, std::initializer_list<U>&, Args...>와 std::is_copy_constructible_v<std::decay_t<ValueType>>이 모두 true인 경우에만 오버로드 해결에 참여합니다.

템플릿 매개변수

ValueType - 포함된 값 타입
타입 요구사항
-
std::decay_t<ValueType> 는 CopyConstructible 요구사항을 충족해야 합니다.

반환값

새로운 포함된 객체에 대한 참조입니다.

예외

T 의 생성자가 던지는 모든 예외를 던집니다. 예외가 발생하면, 이전에 포함된 객체(있는 경우)는 파괴되며, * this 는 값을 포함하지 않습니다.

예제

#include <algorithm>
#include <any>
#include <iostream>
#include <string>
#include <vector>
class Star
{
    std::string name;
    int id;
public:
    Star(std::string name, int id) : name{name}, id{id}
    {
        std::cout << "Star::Star(string, int)\n";
    }
    void print() const
    {
        std::cout << "Star{\"" << name << "\" : " << id << "};\n";
    }
};
int main()
{
    std::any celestial;
    // (1) emplace(Args&&... args);
    celestial.emplace<Star>("Procyon", 2943);
    const auto* star = std::any_cast<Star>(&celestial);
    star->print();
    std::any av;
    // (2) emplace(std::initializer_list<U> il, Args&&... args);
    av.emplace<std::vector<char>>({'C', '+', '+', '1', '7'} /* no args */);
    std::cout << av.type().name() << '\n';
    const auto* va = std::any_cast<std::vector<char>>(&av);
    std::for_each(va->cbegin(), va->cend(), [](char const& c) { std::cout << c; });
    std::cout << '\n';
}

가능한 출력:

Star::Star(string, int)
Star{"Procyon" : 2943};
St6vectorIcSaIcEE
C++17

참고 항목

any 객체를 생성합니다
(public member function)
포함된 객체를 파괴합니다
(public member function)