std::varian...lternative_t(3)

πŸ“¦ version 2024.07.05, in nixpkgs unstable, opensuse-leap 16.0

✨ std::variant_alternative, std::variant_alternative_t ✨

okay bestie, welcome to the <variant> glow-up πŸ’… this page is from the C++ Standard Library manual (the cppreference one), and it's about peeking at a variant's alternatives at compile time β€” no runtime shenanigans, no cap.


🌸 NAME 🌸

std::variant_alternative, std::variant_alternative_t β€” literally the same thing twice because that's the whole name, bestie, we're not gonna pretend otherwise 🫢


πŸ¦‹ Synopsis πŸ¦‹

bestie. it's Defined in header <variant> ✨ and here come the templates, all of them, exactly as written:

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>;
⚠️ heads up: that last one β€” variant_alternative<I, const volatile T> β€” is deprecated in C++20. use it and the compiler's gonna side-eye you πŸ™ˆ

So what's the vibe? This thing gives you compile-time indexed access to the types of the alternatives of the (possibly cv-qualified) variant β€” and it does the cute little trick of combining the cv-qualifications of the variant (if there are any) with the cv-qualifications of the alternative. iykyk πŸ”₯

Formally, bestie, listen:

  • 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 types πŸŽ€

  • type β€” the type of the Ith alternative of the variant, where I must be in [0, sizeof...(Types)), otherwise the program is ill-formed πŸ’€ (respect the bounds, bestie, that's the whole assignment)

🌈 Helper template alias 🌈

template <size_t I, class T>
using variant_alternative_t = typename variant_alternative<I, T>::type;

(since C++17) ✨ β€” yes, _t is the short one, the one you'll actually type eighty times a day. iconic behavior honestly πŸ‘‘


⭐ Example ⭐

// Run this code β€” go on, run it, it's cute:

#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";
}

πŸ“ Output πŸ“

All static assertions passed.

the way those static_asserts all just passed? main character energy fr fr 🌟


πŸ’œ Defect reports πŸ’œ

The following behavior-changing defect reports were applied retroactively to previously published C++ standards β€” yes, retroactively, they went back in time, slay πŸ•°οΈβœ¨

  • LWG 2974 β€” applied to C++17. Behavior as published: an out-of-bounds index resulted in undefined behavior. Correct behavior: made ill-formed. glow-up of the century, honestly πŸ’…

🌸 See also 🌸

  • variant_size, variant_size_v (C++17) β€” obtains the size of the variant's list of alternatives at compile time πŸ”’ (class template) (variable template)
  • std::tuple_element<std::tuple> (C++11) β€” obtains the type of the specified element 🧚 (class template specialization)

okay that's the whole page, bestie, we got every template, every qualification, every defect report, nothing skipped πŸ«ΆπŸ’–

source: http://cppreference.com β€” version dated 2024.06.10 β€” mwah, go touch some std::variants and stay bounded βœ¨πŸŽ€πŸ¦‹

πŸ“¦ where this page comes from