std::varian...riant_size_v(3)

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

πŸ’– NAME πŸ’–

std::variant_size,std::variant_size_v - std::variant_size,std::variant_size_v

Okay bestie, this is the page for std::variant_size and std::variant_size_v from the C++ Standard Libary β€” yes, that's the manual name as the header spells it, folded in right here instead of doing a boring copy-paste. It's giving "count the alternatives in a variant, at compile time" energy. ✨

✨ Synopsis ✨

Defined in header <variant>
template< class T >                                        (1) (since C++17)
struct variant_size; /* undefined */
template< class... Types >

struct variant_size<std::variant<Types...>>                (2) (since C++17)
    : std::integral_constant<std::size_t, sizeof...(Types)> {};
template< class T >                                        (3) (since C++17)
class variant_size<const T>;
template< class T >                                        (4) (since C++17)
class variant_size<volatile T>;
(deprecated in C++20)
template< class T >                                        (5) (since C++17)
class variant_size<const volatile T>;
(deprecated in C++20)

Okay so what does it actually do? It provides access to the number of alternatives in a possibly cv-qualified variant as a compile-time constant expression. 🫢

Formally, bestie:

  • 2) meets the UnaryTypeTrait requirements with a base characteristic of std::integral_constant<std::size_t, sizeof...(Types)>.
  • 3-5) meets the UnaryTypeTrait requirements with a base characteristic of std::integral_constant<std::size_t, std::variant_size<T>::value>.
⚠️ Heads up: specializations (4) and (5) are deprecated in C++20. The synopsis says it, and I'm saying it again because we do not ignore deprecation red flags. πŸ’€

🌸 Member constants 🌸

  • value β€” sizeof...(Types) [static] (public static member constant).

πŸŽ€ Member functions πŸŽ€

  • operator std::size_t β€” converts the object to std::size_t, returns value (public member function).
  • operator() β€” returns value (C++14) (public member function).

πŸ¦‹ Member types πŸ¦‹

  • value_type (under the Type column) β€” Definition: std::size_t.
  • type (under the Type column) β€” Definition: std::integral_constant<std::size_t, value>.

⭐ Notes ⭐

All specializations of std::variant_size satisfy UnaryTypeTrait with base characteristic std::integral_constant<std::size_t, N> for some N. No cap. πŸ’…

πŸ”₯ Example πŸ”₯

// Run this code

#include <any>
#include <variant>

static_assert(std::variant_size_v<std::variant<>> == 0);
static_assert(std::variant_size_v<std::variant<int>> == 1);
static_assert(std::variant_size_v<std::variant<int, int>> == 2);
static_assert(std::variant_size_v<std::variant<int, int, int>> == 3);
static_assert(std::variant_size_v<std::variant<int, float, double>> == 3);
static_assert(std::variant_size_v<std::variant<std::monostate, void>> == 2);
static_assert(std::variant_size_v<std::variant<const int, const float>> == 2);
static_assert(std::variant_size_v<std::variant<std::variant<std::any>>> == 1);

int main() {}

πŸ‘‘ See also πŸ‘‘

  • variant_alternative, variant_alternative_t β€” obtains the type of the alternative specified by its index, at compile time (C++17); variant_alternative is a class template, and variant_alternative_t is an alias template.
  • std::tuple_size<std::tuple> β€” obtains the size of tuple at compile time (C++11) (class template specialization).

β€” signed with glitter and good vibes, from http://cppreference.com, version 2024.06.10, on the std::varian...riant_size_v(3) page πŸ«ΆπŸ’–πŸŒˆ

πŸ“¦ where this page comes from