Namespaces
Variants
Views
Actions

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

From cppreference.com
< cpp‎ | utility‎ | variant
m (langlinks)
m (Parameters: split in two sections.)
 
(4 intermediate revisions by 2 users not shown)
Line 1: Line 1:
{{cpp/title|get_if}}
+
{{cpp/title|get_if {{small|(std::variant)}}}}
 
{{cpp/utility/variant/navbar}}
 
{{cpp/utility/variant/navbar}}
 
{{dcl begin}}
 
{{dcl begin}}
{{dcl header | variant}}
+
{{dcl header|variant}}
{{dcl rev begin | num=1 | since=c++17 }}
+
{{dcl rev begin|num=1|since=c++17}}
{{dcl |
+
{{dcl|
 
template< std::size_t I, class... Types >
 
template< std::size_t I, class... Types >
 
constexpr std::add_pointer_t<std::variant_alternative_t<I, std::variant<Types...>>>
 
constexpr std::add_pointer_t<std::variant_alternative_t<I, std::variant<Types...>>>
 
     get_if( std::variant<Types...>* pv ) noexcept;
 
     get_if( std::variant<Types...>* pv ) noexcept;
 
}}
 
}}
{{dcl |  
+
{{dcl|
 
template< std::size_t I, class... Types >
 
template< std::size_t I, class... Types >
constexpr std::add_pointer_t<const std::variant_alternative_t<I, std::variant<Types...>>>  
+
constexpr std::add_pointer_t<const std::variant_alternative_t<I, std::variant<Types...>>>
 
     get_if( const std::variant<Types...>* pv ) noexcept;
 
     get_if( const std::variant<Types...>* pv ) noexcept;
 
}}
 
}}
 
{{dcl rev end}}
 
{{dcl rev end}}
{{dcl rev begin | num=2 | since=c++17 }}
+
{{dcl rev begin|num=2|since=c++17}}
{{dcl |  
+
{{dcl|
 
template< class T, class... Types >
 
template< class T, class... Types >
constexpr std::add_pointer_t<T> get_if( std::variant<Types...>* pv ) noexcept;
+
constexpr std::add_pointer_t<T>
 +
    get_if( std::variant<Types...>* pv ) noexcept;
 
}}
 
}}
{{dcl |
+
{{dcl|
 
template< class T, class... Types >
 
template< class T, class... Types >
constexpr std::add_pointer_t<const T> get_if( const std::variant<Types...>* pv ) noexcept;
+
constexpr std::add_pointer_t<const T>
 +
    get_if( const std::variant<Types...>* pv ) noexcept;
 
}}
 
}}
 
{{dcl rev end}}
 
{{dcl rev end}}
 
{{dcl end}}
 
{{dcl end}}
  
@1@ Index-based non-throwing accessor: If {{tt|pv}} is not a null pointer and {{c|pv->index() {{==}} I}}, returns a pointer to the value stored in the variant pointed to by {{tt|pv}}. Otherwise, returns a null pointer value. The call is ill-formed if {{tt|I}} is not a valid index in the variant.
+
@1@ Index-based non-throwing accessor: If {{c|pv}} is not a null pointer and {{c|1=pv->index() == I}}, returns a pointer to the value stored in the variant pointed to by {{c|pv}}. Otherwise, returns a null pointer value. The call is ill-formed if {{tt|I}} is not a valid index in the variant.
@2@ Type-based non-throwing accessor: Equivalent to {{v|1}} with {{tt|I}} being the zero-based index of {{tt|T}} in {{tt|Types...}}. The call is ill-formed if {{tt|T}} is not a unique element of {{tt|Types...}}.
+
@2@ Type-based non-throwing accessor: Equivalent to {{v|1}} with {{tt|I}} being the zero-based index of {{tt|T}} in {{c|Types...}}. The call is ill-formed if {{tt|T}} is not a unique element of {{c|Types...}}.
 +
 
 +
===Template parameters===
 +
{{par begin}}
 +
{{par|I|index to look up}}
 +
{{par|Type|unique type to look up}}
 +
{{par end}}
  
 
===Parameters===
 
===Parameters===
 
{{par begin}}
 
{{par begin}}
{{par | I | index to look up }}
+
{{par|pv|pointer to a variant}}
{{par | Type | unique type to look up }}
+
{{par | pv | pointer to a variant}}
+
 
{{par end}}
 
{{par end}}
  
Line 42: Line 48:
 
===Example===
 
===Example===
 
{{example|code=
 
{{example|code=
#include <variant>
 
 
#include <iostream>
 
#include <iostream>
+
#include <variant>
 +
 
 
int main()
 
int main()
 
{
 
{
     std::variant<int, float> v{12};
+
     auto check_value = [](const std::variant<int, float>& v)
 +
    {
 +
        if (const int* pval = std::get_if<int>(&v))
 +
            std::cout << "variant value: " << *pval << '\n';
 +
        else
 +
            std::cout << "failed to get value!" << '\n';
 +
    };
  
     if(auto pval = std::get_if<int>(&v))
+
     std::variant<int, float> v{12}, w{3.f};
      std::cout << "variant value: " << *pval << '\n';  
+
    check_value(v);
     else
+
     check_value(w);
      std::cout << "failed to get value!" << '\n';  
+
 
}
 
}
 
|output=
 
|output=
 
variant value: 12
 
variant value: 12
 +
failed to get value!
 
}}
 
}}
  
 
===See also===
 
===See also===
 
{{dsc begin}}
 
{{dsc begin}}
{{dsc inc | cpp/utility/variant/dsc get}}
+
{{dsc inc|cpp/utility/variant/dsc get}}
 
{{dsc end}}
 
{{dsc end}}
  
 
{{langlinks|es|ja|ru|zh}}
 
{{langlinks|es|ja|ru|zh}}

Latest revision as of 15:39, 25 June 2024

 
 
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::add_pointer_t<std::variant_alternative_t<I, std::variant<Types...>>>

    get_if( std::variant<Types...>* pv ) noexcept;
template< std::size_t I, class... Types >

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

    get_if( const std::variant<Types...>* pv ) noexcept;
(2) (since C++17)
template< class T, class... Types >

constexpr std::add_pointer_t<T>

    get_if( std::variant<Types...>* pv ) noexcept;
template< class T, class... Types >

constexpr std::add_pointer_t<const T>

    get_if( const std::variant<Types...>* pv ) noexcept;
1) Index-based non-throwing accessor: If pv is not a null pointer and pv->index() == I, returns a pointer to the value stored in the variant pointed to by pv. Otherwise, returns a null pointer value. The call is ill-formed if I is not a valid index in the variant.
2) Type-based non-throwing accessor: Equivalent to (1) with I being the zero-based index of T in Types.... The call is ill-formed if T is not a unique element of Types....

Contents

[edit] Template parameters

I - index to look up
Type - unique type to look up

[edit] Parameters

pv - pointer to a variant

[edit] Return value

Pointer to the value stored in the pointed-to variant or null pointer on error.

[edit] Example

#include <iostream>
#include <variant>
 
int main()
{
    auto check_value = [](const std::variant<int, float>& v)
    {
        if (const int* pval = std::get_if<int>(&v))
            std::cout << "variant value: " << *pval << '\n';
        else
            std::cout << "failed to get value!" << '\n';
    };
 
    std::variant<int, float> v{12}, w{3.f};
    check_value(v);
    check_value(w);
}

Output:

variant value: 12
failed to get value!

[edit] See also

reads the value of the variant given the index or the type (if the type is unique), throws on error
(function template) [edit]