Namespaces
Variants

std::any:: any

From cppreference.net
Utilities library
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 가 비어 있는 경우 빈 상태가 됩니다. 공식적으로,
2) 만약 other 가 비어 있다면, 생성된 객체는 비어 있습니다. 그렇지 않으면, any ( std:: in_place_type < T > , std:: any_cast < const T & > ( other ) ) 와 동등하며, 여기서 T other 에 포함된 객체의 타입입니다.
3) 만약 other 가 비어 있으면, 생성된 객체는 비어 있습니다. 그렇지 않으면, 생성된 객체는 other 에 포함된 객체를 포함하거나, other 에 포함된 객체를 rvalue로 간주하여 동일한 타입의 객체로부터 생성된 객체를 포함합니다.
4) 초기 내용으로 std:: decay_t < ValueType > 타입의 객체를 가지는 객체를 생성하며, 직접 초기화 방식으로 std:: forward < ValueType > ( value ) 에서 초기화됩니다.
5) std:: decay_t < ValueType > 타입의 객체를 초기 내용으로 가지는 객체를 생성하며, 이 객체는 std:: forward < Args > ( args ) ... 에서 직접-비목록-초기화(direct-non-list-initialized) 됩니다.
6) 초기 내용으로 std:: decay_t < ValueType > 타입의 객체를 가지는 객체를 생성하며, 이는 il, std:: forward < Args > ( args ) ... 에서 직접-비목록-초기화 됩니다.

목차

템플릿 매개변수

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)