
- 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 visit() Function
The visit() function lets you apply a callable object, such as a function or a lambda, to a set of std::variant instances. It requires two types of input: a Visitor (the callable object) and multiple std::variant objects. When you call visit(), it determines which variant holds a value and then calls the Visitor with the values extracted from those variants
This allows you to handle different types of data in a unified way, making your code cleaner and easier to manage, especially when dealing with complex type situations. This approach is commonly used in scenarios like pattern matching or when implementing custom behavior based on the actual types stored in the variants.
Syntax
Following is the syntax for variant visit() function.
template <class Visitor, class... Variants> constexpr decltype(auto) visit(Visitor&& vis, Variants&&... vars);
Parameters
Visitor - A callable object (lambda or function) that defines the operation to apply.
Variants - One or more std::variant objects to which the visitor is applied.
Return Value
The function returns the result of the visitor function applied to the value stored in the variant.
Time Complexity
The time complexity of this function is constant, O(1) as it only checks the active index of the variant and applies the callable.
Example 1
#include <iostream> #include <variant> #include <string> int main() { std::variant<int, std::string> v = "hello"; auto visitor = [](auto&& arg) { std::cout << arg << std::endl; }; std::visit(visitor, v); return 0; }
Output
Output of the above code is as follows
hello
Example 2
In this example, std::visit() is used to handle different types stored in a variant. The function std::visit() checks the type of the stored value and prints it accordingly. In this case, the variant holds an integer value.
#include <iostream> #include <variant> #include <string> int main() { std::variant<int, std::string> v = 42; auto visitor = [](auto&& arg) { if constexpr (std::is_same_v<decltype(arg), int>) { std::cout << "int: " << arg << std::endl; } else { std::cout << "string: " << arg << std::endl; } }; std::visit(visitor, v); return 0; }
Output
If we run the above code it will generate the following output
string: 42
Example 3
This example shows how to use std::visit with two std::variant objects at the same time. The lambda function sums the held values. Regardless of whether the types are int or double, std::visit functions smoothly.
#include <iostream> #include <variant> int main() { std::variant<int, double< var1 = 5; std::variant<int, double> var2 = 2.5; auto result = std::visit([](auto&& val1, auto&& val2) { return val1 + val2; }, var1, var2); std::cout << "Result: " << result << '\n'; return 0; }
Output
Following is the output of the above code
Result: 7.5