std::any:: emplace
From cppreference.net
|
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
)
...
에서 포함된 객체로 초기화합니다.
- 이 오버로드는 다음 조건이 모두 true 인 경우에만 오버로드 해결에 참여합니다: std:: is_constructible_v < std:: decay_t < ValueType > , Args... > 와 std:: is_copy_constructible_v < std:: decay_t < ValueType >> .
2)
std::
decay_t
<
ValueType
>
타입의 객체를 생성하며,
직접-비목록-초기화
방식으로
il,
std::
forward
<
Args
>
(
args
)
...
에서 포함된 객체로 초기화합니다.
- 이 오버로드는 다음 조건이 모두 true 일 때만 오버로드 해결에 참여합니다: std:: is_constructible_v < std:: decay_t < ValueType > , std:: initializer_list < U > & , Args... > 와 std:: is_copy_constructible_v < std:: decay_t < ValueType >> .
목차 |
템플릿 매개변수
| 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) |