Search⌘ K
AI Features

Visitors for std::variant

Explore how to use std::visit and visitor patterns to operate on std::variant types in C++17. Understand visitor declarations, overloads for different variant types, and learn to implement both generic lambdas and structured visitors for flexible type handling.

We'll cover the following...

With the introduction of std::variant, we also got a handy STL function called std::visit. It can call a given “visitor” on all passed variants.

Visitor Declaration

C++
template <class Visitor, class... Variants>
constexpr visit(Visitor&& vis, Variants&&... vars);

And it will call vis on the currently active type of variants.

If you ...