
- 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 get() Function
The std::get() function is used to access the value stored in a std::variant object. The std::get() function can be used with an index or a type to retrieve the currently stored value from a std::variant. If an invalid index or type is accessed, it throws std::bad_variant_access.
This function is useful when working with variants, ensuring type safety and flexibility in handling multiple types within a single variable.
Syntax
Following is the syntax for variant std::get() function.
// Get value by index std::get<N>(std::variant<Types...> &var); std::get<N>(const std::variant<Types...> &var); std::get<N>(std::variant<Types...> &&var); std::get<N>(const std::variant<Types...> &&var); // Get value by type std::get<Type>(std::variant<Types...> &var); std::get<Type>(const std::variant<Types...> &var); std::get<Type>(std::variant<Types...> &&var); std::get<Type>(const std::variant<Types...> &&var);
Parameters
- N : The index of the type stored in the std::variant
- Type : The exact type stored in the std::variant.
- var : The std::variant object from which the value is retrieved.
Return Value
The function returns the value stored in the std::variant. If an incorrect index or type is accessed, it throws a std::bad_variant_access exception.
Time Complexity
The time complexity of this function is constant, i.e., O(1).
Example 1
The following example demonstrate, accessing the value by its index using the std::get() which retrieves and prints the value using the index.
#include <iostream> #include <variant> int main() { std::variant<int, double, std::string> var = 42; std::cout << "Value at index 0: " << std::get<0>(var) << std::endl; return 0; }
Output
Output of the above code is as follows
Value at index 0: 42
Example 2
In the following example, we are going to access the value by its type using get() function, which retrieves the stored value using the type.
#include <iostream> #include <variant> int main() { std::variant<int, double, std::string> var = 3.14; std::cout << "Value stored: " << std::get<double>(var) << std::endl; return 0; }
Output
If we run the above code it will generate the following output
Value stored: 3.14
Example 3
Here, we are trying to access an integer using std::get<int>(var) results in an exception, which is caught and displayed.
#include <iostream> #include <variant> #include <stdexcept> int main() { std::variant<int, double, std::string> var = "Hello"; try { std::cout << std::get<int>(var) << std::endl; // Incorrect type access } catch (const std::bad_variant_access &e) { std::cerr << "Exception caught: " << e.what() << std::endl; } return 0; }
Output
Following is the output of the above code
Exception caught: bad_variant_access