std::varian...lternative_t(3)
β¨ 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 typedeftypeequal to the type of the alternative with indexIβ¨3)meets the TransformationTrait requirements with a member typedeftypethat names, respectively,std::add_const_t<std::variant_alternative_t<I,T>>,std::add_volatile_t<std::variant_alternative_t<I,T>>, andstd::add_cv_t<std::variant_alternative_t<I,T>>π
π Member types π
typeβ the type of theIth alternative of the variant, whereImust 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
- nixpkgs unstable: stdman 2024.07.05
- opensuse-leap 16.0: stdman 2024.07.05-bp160.1.9 Β· MIT
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash-1017 on : a language model's rewrite of the original, under its terms