std::any:: any
|
constexpr
any
(
)
noexcept
;
|
(1) | (C++17부터) |
|
any
(
const
any
&
other
)
;
|
(2) | (C++17부터) |
|
any
(
any
&&
other
)
noexcept
;
|
(3) | (C++17부터) |
|
template
<
class
ValueType
>
any ( ValueType && value ) ; |
(4) | (C++17부터) |
|
template
<
class
ValueType,
class
...
Args
>
explicit any ( std:: in_place_type_t < ValueType > , Args && ... args ) ; |
(5) | (C++17부터) |
|
template
<
class
ValueType,
class
U,
class
...
Args
>
explicit
any
(
std::
in_place_type_t
<
ValueType
>
,
std::
initializer_list
<
U
>
il,
|
(6) | (C++17부터) |
새로운
any
객체를 생성합니다.
T
는
other
에 포함된 객체의 타입입니다.
-
이 오버로드는
std::
decay_t
<
ValueType
>
가
any타입과 동일하지 않고 std::in_place_type_t 의 특수화도 아니며, std:: is_copy_constructible_v < std:: decay_t < ValueType >> 가 true 인 경우에만 오버로드 해결에 참여합니다.
- 이 오버로드는 다음 조건이 모두 true 인 경우에만 오버로드 해결에 참여합니다: std:: is_constructible_v < std:: decay_t < ValueType > , Args... > 와 std:: is_copy_constructible_v < std:: decay_t < ValueType >> .
- 이 오버로드는 다음 조건이 모두 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
요구사항을 충족해야 합니다.
|
||
매개변수
| other | - |
복사 또는 이동할 다른
any
객체
|
| value | - | 포함된 값을 초기화하는 데 사용할 값 |
| il, args | - | 포함된 객체의 생성자에 전달할 인수들 |
예외
참고 사항
기본 생성자가
constexpr
이므로, 정적
std::any
는
정적 비지역 변수 초기화
의 일부로 초기화되며, 이는 동적 비지역 초기화가 시작되기 전에 수행됩니다. 이로 인해 어떤 정적 객체의 생성자에서도
std::any
타입의 객체를 안전하게 사용할 수 있습니다.
예제
#include <boost/core/demangle.hpp> #include <any> #include <initializer_list> #include <iostream> #include <memory> #include <set> #include <string> #include <utility> struct A { int age; std::string name; double salary; #if __cpp_aggregate_paren_init < 201902L // C++20 이전에는 제자리 생성에 필요함 A(int age, std::string name, double salary) : age(age), name(std::move(name)), salary(salary) {} #endif }; // abi demangle을 사용하여 any가 보유한 인스턴스의 정확한 타입 이름 출력 void printType(const std::any& a) { std::cout << boost::core::demangle(a.type().name()) << '\n'; } int main() { // 생성자 #4: int를 보유하는 std::any std::any a1{7}; // 생성자 #5: 제자리에서 생성된 A를 보유하는 std::any std::any a2(std::in_place_type<A>, 30, "Ada", 1000.25); // 생성자 #6: 사용자 정의 비교 함수를 가진 A의 set을 보유하는 std::any auto lambda = [](auto&& l, auto&& r){ return l.age < r.age; }; std::any a3( std::in_place_type<std::set<A, decltype(lambda)>>, { A{39, std::string{"Ada"}, 100.25}, A{20, std::string{"Bob"}, 75.5} }, lambda); printType(a1); printType(a2); printType(a3); }
가능한 출력:
int
A
std::set<A, main::{lambda(auto:1&&, auto:2&&)#1}, std::allocator<A> >
참고 항목
any
객체를 할당함
(public member function) |