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

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

In this example, std::visit() executes a function on a value stored in a variant, which can hold different types. In this case, the variant contains a std::string. The purpose of std::visit() is to apply a visitor function that prints the value stored in the variant.
#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
cpp_variant.htm
Advertisements