std::any::any
cppreference.net에서
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,
Args&&... args );
|
(6) | (C++17부터) |
새 any 객체를 생성합니다.
1) 빈 객체를 생성합니다.
2,3) 의 내용을 복사 (2)하거나 이동 (3)
other하여 새 인스턴스를 생성합니다. 생성된 객체는 생성자 호출 전의 other 와 동일한 타입과 값을 가지거나, other 가 비어 있는 경우 비어 있게 됩니다. 공식적으로,2)
other 가 비어 있으면 생성된 객체는 비어 있습니다. 그렇지 않으면 any(std::in_place_type<T>, std::any_cast<const T&>(other)) 와 동일하며, 여기서 T 는 other 에 포함된 객체의 타입입니다.3)
other 가 비어 있으면 생성된 객체는 비어 있습니다. 그렇지 않으면 생성된 객체는 other 에 포함된 객체를 포함하거나, 해당 객체를 rvalue로 간주하여 other 에 포함된 객체로부터 생성된 동일한 타입의 객체를 포함합니다.4)
std::decay_t<ValueType> 타입의 객체를 초기 내용으로 하는 객체를 생성하며, 직접 초기화를 std::forward<ValueType>(value) 로부터 수행합니다.
- 이 오버로드는
std::decay_t<ValueType>가any과 동일한 타입이 아니고 std::in_place_type_t의 특수화가 아니며std::is_copy_constructible_v<std::decay_t<ValueType>>가true인 경우에만 오버로드 해결에 참여합니다.
5)
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인 경우에만 오버로드 해결에 참여합니다.
6)
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
요구사항을 충족해야 합니다.
|
||
매개변수
| other | - |
복사 또는 이동할 다른
any
객체
|
| value | - | 포함된 값을 초기화하는 데 사용할 값 |
| il, args | - | 포함된 객체의 생성자에 전달할 인수들 |
예외
2,4-6)
포함된 타입의 생성자가 던지는 모든 예외를 발생시킵니다.
참고 사항
기본 생성자가
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) |