
- C++ Library - Home
- C++ Library - <fstream>
- C++ Library - <iomanip>
- C++ Library - <ios>
- C++ Library - <iosfwd>
- C++ Library - <iostream>
- C++ Library - <istream>
- C++ Library - <ostream>
- C++ Library - <sstream>
- C++ Library - <streambuf>
- C++ Library - <atomic>
- C++ Library - <complex>
- C++ Library - <exception>
- C++ Library - <functional>
- C++ Library - <limits>
- C++ Library - <locale>
- C++ Library - <memory>
- C++ Library - <new>
- C++ Library - <numeric>
- C++ Library - <regex>
- C++ Library - <stdexcept>
- C++ Library - <string>
- C++ Library - <thread>
- C++ Library - <tuple>
- C++ Library - <typeinfo>
- C++ Library - <utility>
- C++ Library - <valarray>
- The C++ STL Library
- C++ Library - <array>
- C++ Library - <bitset>
- C++ Library - <deque>
- C++ Library - <forward_list>
- C++ Library - <list>
- C++ Library - <map>
- C++ Library - <multimap>
- C++ Library - <queue>
- C++ Library - <priority_queue>
- C++ Library - <set>
- C++ Library - <stack>
- C++ Library - <unordered_map>
- C++ Library - <unordered_set>
- C++ Library - <vector>
- C++ Library - <algorithm>
- C++ Library - <iterator>
- The C++ Advanced Library
- C++ Library - <any>
- C++ Library - <barrier>
- C++ Library - <bit>
- C++ Library - <chrono>
- C++ Library - <cinttypes>
- C++ Library - <clocale>
- C++ Library - <condition_variable>
- C++ Library - <coroutine>
- C++ Library - <cstdlib>
- C++ Library - <cstring>
- C++ Library - <cuchar>
- C++ Library - <charconv>
- C++ Library - <cfenv>
- C++ Library - <cmath>
- C++ Library - <ccomplex>
- C++ Library - <expected>
- C++ Library - <format>
- C++ Library - <future>
- C++ Library - <flat_set>
- C++ Library - <flat_map>
- C++ Library - <filesystem>
- C++ Library - <generator>
- C++ Library - <initializer_list>
- C++ Library - <latch>
- C++ Library - <memory_resource>
- C++ Library - <mutex>
- C++ Library - <mdspan>
- C++ Library - <optional>
- C++ Library - <print>
- C++ Library - <ratio>
- C++ Library - <scoped_allocator>
- C++ Library - <semaphore>
- C++ Library - <source_location>
- C++ Library - <span>
- C++ Library - <spanstream>
- C++ Library - <stacktrace>
- C++ Library - <stop_token>
- C++ Library - <syncstream>
- C++ Library - <system_error>
- C++ Library - <string_view>
- C++ Library - <stdatomic>
- C++ Library - <variant>
- C++ STL Library Cheat Sheet
- C++ STL - Cheat Sheet
- C++ Programming Resources
- C++ Programming Tutorial
- C++ Useful Resources
- C++ Discussion
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
#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