std::holds_alternative(3)
std::holds_alternative(3) π
okay so bestie, this page is from the C++ Standard Libary manual, and itβs all about checking whether a std::variant is currently holding one specific alternative. Itβs giving type-safe vibe check, fr fr. β¨
β¨ NAME β¨
std::holds_alternativeβstd::holds_alternative(thatβs the name, no cap)
π¦ Synopsis π¦
Defined in header <variant>, and itβs been iconic since C++17:
Defined in header <variant>
template< class T, class... Types >
constexpr bool holds_alternative( const std::variant<Types...>& v )
(since C++17)
noexcept;
Checks if the variant v holds the alternative T.
The call is ill-formed ifTdoes not appear exactly once inTypes.... Exactly once, periodt. π
π Parameters π
vβ the variant to examine. Sheβs the main character here. π
β¨ Return value β¨
true if the variant currently holds the alternative T, false otherwise. Slay. π
π Example π
Run this code, bestie:
// Run this code
#include <variant>
#include <string>
#include <iostream>
int main()
{
std::variant<int, std::string> v = "abc";
std::cout << std::boolalpha
<< "variant holds int? "
<< std::holds_alternative<int>(v) << '\n'
<< "variant holds string? "
<< std::holds_alternative<std::string>(v) << '\n';
}
Output:
variant holds int? false
variant holds string? true
π See also π
indexβ returns the zero-based index of the alternative held by the variant (public member function)get(std::variant)(C++17) β reads the value of the variant given the index or the type (if the type is unique), throws on error (function template)
mwah π source/version/date: http://cppreference.com, 2024.06.10, std::holds_alternative(3). π«Ά
π¦ 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