Namespaces
Variants
Views
Actions

Difference between revisions of "cpp/utility/variant/get"

From cppreference.com
< cpp‎ | utility‎ | variant
(fix T and Types)
(fmt)
Line 5: Line 5:
 
{{dcl rev begin | since=c++17 | num=1 }}
 
{{dcl rev begin | since=c++17 | num=1 }}
 
{{dcl|
 
{{dcl|
template <std::size_t I, class... Types>
+
template< std::size_t I, class... Types >
constexpr std::variant_alternative_t<
+
constexpr std::variant_alternative_t<I, std::variant<Types...>>&  
  I, std::variant<Types...>
+
    get(std::variant<Types...>& v);  
>& get(std::variant<Types...>& v); }}
+
}}
 
{{dcl|
 
{{dcl|
template <std::size_t I, class... Types>
+
template< std::size_t I, class... Types >
constexpr std::variant_alternative_t<
+
constexpr std::variant_alternative_t<I, std::variant<Types...>>&&  
  I, std::variant<Types...>
+
    get(std::variant<Types...>&& v);  
>&& get(std::variant<Types...>&& v); }}
+
}}
 
{{dcl|
 
{{dcl|
template <std::size_t I, class... Types>
+
template< std::size_t I, class... Types >
constexpr std::variant_alternative_t<
+
constexpr const std::variant_alternative_t<I, std::variant<Types...>>&  
  I, std::variant<Types...>
+
    get(const std::variant<Types...>& v);  
> const& get(const std::variant<Types...>& v); }}
+
}}
 
{{dcl|
 
{{dcl|
template <std::size_t I, class... Types>
+
template< std::size_t I, class... Types >
constexpr std::variant_alternative_t<
+
constexpr const std::variant_alternative_t<I, std::variant<Types...>>&
  I, std::variant<Types...>
+
    get( const std::variant<Types...>&& v );
> const&& get(const std::variant<Types...>&& v);}}
+
}}
 
{{dcl rev end}}
 
{{dcl rev end}}
 
{{dcl rev begin | since=c++17 | num=2 }}
 
{{dcl rev begin | since=c++17 | num=2 }}
 
{{dcl|
 
{{dcl|
template <class T, class... Types>
+
template< class T, class... Types >
constexpr T& get(std::variant<Types...>& v); }}
+
constexpr T& get( std::variant<Types...>& v );  
 +
}}
 
{{dcl|
 
{{dcl|
template <class T, class... Types>
+
template< class T, class... Types >
constexpr T&& get(std::variant<Types...>&& v); }}
+
constexpr T&& get( std::variant<Types...>&& v );  
 +
}}
 
{{dcl|
 
{{dcl|
template <class T, class... Types>
+
template< class T, class... Types >
constexpr const T& get(const std::variant<Types...>& v); }}
+
constexpr const T& get( const std::variant<Types...>& v );  
 +
}}
 
{{dcl|
 
{{dcl|
template <class T, class... Types>
+
template< class T, class... Types >
constexpr const T&& get(const std::variant<Types...>&& v); }}
+
constexpr const T&& get( const std::variant<Types...>&& v );  
 +
}}
 
{{dcl rev end}}
 
{{dcl rev end}}
 
{{dcl end}}
 
{{dcl end}}

Revision as of 15:07, 29 October 2019

 
 
Utilities library
General utilities
Relational operators (deprecated in C++20)
 
 
Defined in header <variant>
(1) (since C++17)
template< std::size_t I, class... Types >

constexpr std::variant_alternative_t<I, std::variant<Types...>>&

    get(std::variant<Types...>& v);
template< std::size_t I, class... Types >

constexpr std::variant_alternative_t<I, std::variant<Types...>>&&

    get(std::variant<Types...>&& v);
template< std::size_t I, class... Types >

constexpr const std::variant_alternative_t<I, std::variant<Types...>>&

    get(const std::variant<Types...>& v);
template< std::size_t I, class... Types >

constexpr const std::variant_alternative_t<I, std::variant<Types...>>&

    get( const std::variant<Types...>&& v );
(2) (since C++17)
template< class T, class... Types >
constexpr T& get( std::variant<Types...>& v );
template< class T, class... Types >
constexpr T&& get( std::variant<Types...>&& v );
template< class T, class... Types >
constexpr const T& get( const std::variant<Types...>& v );
template< class T, class... Types >
constexpr const T&& get( const std::variant<Types...>&& v );
1) Index-based value accessor: If v.index() == I, returns a reference to the value stored in v. Otherwise, throws std::bad_variant_access. The call is ill-formed if I is not a valid index in the variant.
2) Type-based value accessor: If v holds the alternative T, returns a reference to the value stored in v. Otherwise, throws std::bad_variant_access. The call is ill-formed if T is not a unique element of Types....

Contents

Template parameters

I - index to look up
T - unique type to look up
Types... - types forming the variant

Parameters

v - a variant

Return value

Reference to the value stored in the variant.

Exceptions

1,2) Throws std::bad_variant_access on errors.

Example

#include <variant>
#include <string>
 
int main()
{
    std::variant<int, float> v{12}, w;
    int i = std::get<int>(v);
    w = std::get<int>(v);
    w = std::get<0>(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 (std::bad_variant_access&) {}
}

See also

(C++17)
obtains a pointer to the value of a pointed-to variant given the index or the type (if unique), returns null on error
(function template) [edit]
tuple accesses specified element
(function template) [edit]
accesses an element of an array
(function template) [edit]
accesses an element of a pair
(function template) [edit]