Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

C++ variant holds_alternative() Function



The std::sholds alternative function allows you to check if a std::variant currently specific alternative type. A specific alternative type refers to one of the types that the std::variant is defined to hold (int, double, string).

Before you try to work with the value stored in the variant, by using std holds_alternative, you can safely check it's type, which helps to avoid errors that can occur from trying to access the wrong type. This makes your code cleaner and safer, especially in complex programs where managing different types is common.

Syntax

Following is the syntax for variant std::holds alternative() function.

template <class T, class... Types>
constexpr bool holds_alternative(const std::variant<Types...>& v) noexcept;

Parameters

  • T - The type to check within the std::variant.
  • v - The std::variant object to examine.

Return Value

The function returns a boolean value:

  • true : If the variant currently holds a value of type T.
  • false : Otherwise.

Time Complexity

The time complexity of this function is constant, i.e., O(1), since it simply checks the currently active type in the variant.

Example 1

In the following example, we are going to check for an integer type in a variant using holds_alternative() function and returns true.
#include <iostream>
#include <variant>
int main() {
   std::variant<int, double, std::string> v = 10;
   
   if (std::holds_alternative<int>(v)) {
      std::cout << "Variant holds an int value." << std::endl;
   }
   return 0;
}

Output

Output of the above code is as follows

Variant holds an int value.

Example 2

The following example demonstrates how to check the string type in a variant by using holds_alternative() function, which returns true.

#include <iostream>
#include <variant>
int main() {
   std::variant<int, double, std::string> v = "Hello";
   
   if (std::holds_alternative<std::string>(v)) {
      std::cout << "Variant holds a string value: " << std::get<std::string>(v) << std::endl;
   }
   return 0;
}

Output

If we run the above code it will generate the following output

Variant holds a string value: Hello

Example 3

This example defines a function checkVariant() that checks the type stored in a std::variant<int, float, char>. It uses std::holds_alternative<T>() to determine the active type and prints the corresponding value.

#include <iostream>
#include <variant>

void checkVariant(const std::variant<int, float, char>& v) {
   if (std::holds_alternative<int>(v)) {
      std::cout << "Variant holds an int: " << std::get<int>(v) << std::endl;
   } else if (std::holds_alternative<float>(v)) {
      std::cout << "Variant holds a float: " << std::get<float>(v) << std::endl;
   } else if (std::holds_alternative<char>(v)) {
      std::cout << "Variant holds a char: " << std::get<char>(v) << std::endl;
   }
}

Output

Following is the output of the above code

Variant holds a char: A
cpp_variant.htm
Advertisements