이름공간
변수

std::variant

cppreference.com
 
 
 
 
<tbody> </tbody>
<variant> 에 정의되어 있음.
template <class... Types> class variant;
(since C++17)

The class template std::variant represents a type-safe union. An instance of std::variant at any given time either holds a value of one of its alternative types, or in the case of error - no value (this state is hard to achieve, see valueless_by_exception).

As with unions, if a variant holds a value of some object type T, the object representation of T is allocated directly within the object representation of the variant itself. Variant is not allowed to allocate additional (dynamic) memory.

A variant is not permitted to hold references, arrays, or the type void. Empty variants are also ill-formed (std::variant<std::monostate> can be used instead).

A variant is permitted to hold the same type more than once, and to hold differently cv-qualified versions of the same type.

Consistent with the behavior of unions during aggregate initialization, a default-constructed variant holds a value of its first alternative, unless that alternative is not default-constructible (in which case the variant is not default-constructible either). The helper class std::monostate can be used to make such variants default-constructible.

Template parameters

Types - the types that may be stored in this variant. All types must meet the Destructible requirements (in particular, array types and non-object types are not allowed).

Member functions

variant객체를 생성합니다
(public member function) [edit]
variant 가 담고 있는 값에 따라 객체를 소멸합니다
(public member function) [edit]
variant를 할당합니다
(public member function) [edit]
Observers
the variant 에서 저장하고 있는 값의 타입의 0 기반 인덱스를 반환합니다
(public member function) [edit]
variant 가 유효하지 않은 상태인지 검사합니다
(public member function) [edit]
Modifiers
variant 의 내부 저장 공간에 값을 바로 생성합니다
(public member function) [edit]
다른 variant와 값을 바꿉니다
(public member function) [edit]

Non-member functions

(C++17)
하나 이상의 variant 인자에 대해 제공된 functor 를 호출합니다
(function template) [edit]
variant 가 주어진 타입값을 저장하고 있는지 검사합니다
(function template) [edit]
주어진 인덱스나 타입(타입이 고유한 경우)의 variant 의 값을 읽거나, 에러가 발생하면 예외를 던집니다
(function template) [edit]
(C++17)
주어진 인덱스나 타입(타입이 고유한 경우)이 가리키는 값의 포인터를 획득하거나, 에러가 발생하면 null 을 반환합니다
(function template) [edit]
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++17)
(C++20)
variant 객체에 저장된 값으로 variant를 비교합니다
(function template) [edit]
std::swap 알고리즘을 특수화합니다
(function template) [edit]

Helper classes

(C++17)
기본 생성을 할 수없는 타입의 variant 에 첫 값으로 사용하기 위한 공간 확보 (placeholder) 타입
(class) [edit]
variant의 값에 유효하지 않은 접근을 하면 발생하는 예외
(class) [edit]
컴파일할 때 variant 의 저장값 목록의 크기를 획득합니다
(class template) (variable template) [edit]
컴파일할 때 인덱스로 주어진 선택값의 타입을 획득합니다
(class template) (alias template) [edit]
std::hash 알고리즘을 특수화합니다
(class template specialization) [edit]

Helper objects

유효하지 않은 상태인 variant 의 인덱스
(constant) [edit]

Example

#include <variant>
#include <string>
#include <cassert>
#include <iostream>

int main()
{
    std::variant<int, float> v, w;
    v = 42; // v contains int
    int i = std::get<int>(v);
    assert(42 == i); // succeeds
    w = std::get<int>(v);
    w = std::get<0>(v); // same effect as the previous line
    w = v; // same effect as the previous line

//  std::get<double>(v); // error: no double in [int, float]
//  std::get<3>(v);      // error: valid index values are 0 and 1

    try {
      std::get<float>(w); // w contains int, not float: will throw
    }
    catch (const std::bad_variant_access& ex) {
        std::cout << ex.what() << '\n';
    }

    using namespace std::literals;

    std::variant<std::string> x("abc");
    // converting constructors work when unambiguous
    x = "def"; // converting assignment also works when unambiguous

    std::variant<std::string, void const*> y("abc");
    // casts to void const * when passed a char const *
    assert(std::holds_alternative<void const*>(y)); // succeeds
    y = "xyz"s;
    assert(std::holds_alternative<std::string>(y)); // succeeds
}

Possible output:

std::get: wrong index for variant

Defect reports

The following behavior-changing defect reports were applied retroactively to previously published C++ standards.

DR Applied to Behavior as published Correct behavior
LWG 2901 C++17 specialization of std::uses_allocator provided, but variant can't properly support allocators specialization removed

See also

(C++14)
an object of type in_place_t
(function) [edit]
(C++14)
객체를 가질수도 가지지 않을 수도 있는 타입
(class template) [edit]
(C++17)
CopyConstructible 타입의 인스턴스를 저장하고 있는 개체
(class) [edit]