GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
std::variant_alternative,std::variant_alternative_t(3) C++ Standard Libary std::variant_alternative,std::variant_alternative_t(3)

std::variant_alternative,std::variant_alternative_t - std::variant_alternative,std::variant_alternative_t


Defined in header <variant>
template <std::size_t I, class T> (1) (since C++17)
struct variant_alternative; /* undefined */
template <std::size_t I, class... Types> (2) (since C++17)
struct variant_alternative<I, variant<Types...>>;
template <std::size_t I, class T> class (3) (since C++17)
variant_alternative<I, const T>;
template <std::size_t I, class T>


class variant_alternative<I, volatile T>; (3) (since C++17)
template <std::size_t I, class T> (deprecated in C++20)


class variant_alternative<I, const volatile T>;


Provides compile-time indexed access to the types of the alternatives of the
possibly cv-qualified variant, combining cv-qualifications of the variant (if any)
with the cv-qualifications of the alternative.


Formally,


2) meets the TransformationTrait requirements with a member typedef type equal to
the type of the alternative with index I
3) meets the TransformationTrait requirements with a member typedef type that names,
respectively, std::add_const_t<std::variant_alternative_t<I,T>>,
std::add_volatile_t<std::variant_alternative_t<I,T>>, and
std::add_cv_t<std::variant_alternative_t<I,T>>


Member type Definition
type the type of Ith alternative of the variant, where I must be in [0,
sizeof...(Types)), otherwise the program is ill-formed.


Helper template alias


template <size_t I, class T>
using variant_alternative_t = typename variant_alternative<I, (since C++17)
T>::type;

// Run this code


#include <variant>
#include <iostream>


using my_variant = std::variant<int, float>;
static_assert(std::is_same_v
<int, std::variant_alternative_t<0, my_variant>>);
static_assert(std::is_same_v
<float, std::variant_alternative_t<1, my_variant>>);
// cv-qualification on the variant type propagates to the extracted alternative type.
static_assert(std::is_same_v
<const int, std::variant_alternative_t<0, const my_variant>>);


int main()
{
std::cout << "All static assertions passed.\n";
}


All static assertions passed.


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 2974 C++17 out-of-bounds index resulted in undefined made ill-formed
behavior


variant_size obtains the size of the variant's list of
variant_size_v alternatives at compile time
(C++17) (class template) (variable template)
std::tuple_element<std::tuple> obtains the type of the specified element
(C++11) (class template specialization)

2022.07.31 http://cppreference.com

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.