std::variant<Types...>::visit
From cppreference.com
C++
Feature test macros (C++20)
Concepts library (C++20)
Metaprogramming library (C++11)
Ranges library (C++20)
Filesystem library (C++17)
Concurrency support library (C++11)
Execution control library (C++26)
Utilities library
Relational operators (deprecated in C++20)
Integer comparison functions
Swap and type operations
Common vocabulary types
Type support (basic types, RTTI)
Library feature-test macros (C++20)
(C++11)
(C++20)
(C++26)
(C++20)
Coroutine support (C++20)
Contract support (C++26)
(C++20)(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
General utilities
(C++20)(C++20)(C++20)
(C++20)(C++20)(C++20)
(C++20)
std::variant
variant::visit
(C++26)
template< class Self, class Visitor >
constexpr decltype(auto) visit( this Self&& self, Visitor&& vis );
(1)
(since C++26)
constexpr decltype(auto) visit( this Self&& self, Visitor&& vis );
template< class R, class Self, class Visitor >
constexpr R visit( this Self&& self, Visitor&& vis );
(2)
(since C++26)
constexpr R visit( this Self&& self, Visitor&& vis );
Applies the visitor vis (a Callable that can be called with any combination of types from the variant) to the variant held by self.
Given type V as decltype(std::forward_like <Self>(std::declval <variant>())), the equivalent call is:
1) return std::visit(std::forward <Visitor>(vis), (V) self);.
2) return std::visit<R>(std::forward <Visitor>(vis), (V) self);.
[edit] Parameters
vis
-
a Callable that accepts every possible alternative from the variant
self
-
variant to pass to the visitor
[edit] Return value
1) The result of the std::visit invocation.
2) Nothing if
R
is (possibly cv-qualified) void; otherwise the result of the std::visit<R> invocation.[edit] Exceptions
Only throws if the call to std::visit throws.
[edit] Notes
Feature-test macro | Value | Std | Feature |
---|---|---|---|
__cpp_lib_variant |
202306L |
(C++26) | member visit
|
[edit] Example
Run this code
#include <print> #include <string> #include <string_view> #include <variant> struct Base {}; struct Derived : Base {}; // helper type for the visitor template<class... Ts> struct overloads : Ts... { using Ts::operator()...; }; // the variant to visit using var_t = std::variant <int, std::string, Derived>; int main() { const auto visitor = overloads { [](int i){ std::print ("int = {}\n", i); }, [](std::string_view s){ std::println ("string = "{}"", s); }, [](const Base&){ std::println ("base"); } }; const var_t var1 = 42, var2 = "abc", var3 = Derived(); #if (__cpp_lib_variant >= 202306L) var1.visit(visitor); var2.visit(visitor); var3.visit(visitor); #else std::visit(visitor, var1); std::visit(visitor, var2); std::visit(visitor, var3); #endif }
Output:
int = 42 string = "abc" base