userver: userver/utils/overloaded.hpp Source File
Loading...
Searching...
No Matches
overloaded.hpp
Go to the documentation of this file.
1#pragma once
2
3/// @file userver/utils/overloaded.hpp
4/// @brief @copybrief utils::Overloaded
5
6#include <variant>
7
8USERVER_NAMESPACE_BEGIN
9
10namespace utils {
11
12/// @ingroup userver_universal
13///
14/// @brief Utility to define std::variant visitors in a simple way
15template <class... Ts>
16struct Overloaded : Ts... { // NOLINT(fuchsia-multiple-inheritance)
17 using Ts::operator()...;
18};
19
20template <class... Ts>
21Overloaded(Ts...) -> Overloaded<Ts...>;
22
23template <class... Args, class... Ts>
24constexpr decltype(auto) Visit(const std::variant<Args...>& var, Ts&&... ts) {
25 return std::visit(Overloaded{std::forward<Ts>(ts)...}, var);
26}
27
28template <class... Args, class... Ts>
29constexpr decltype(auto) Visit(std::variant<Args...>& var, Ts&&... ts) {
30 return std::visit(Overloaded{std::forward<Ts>(ts)...}, var);
31}
32
33template <class... Args, class... Ts>
34constexpr decltype(auto) Visit(std::variant<Args...>&& var, Ts&&... ts) {
35 return std::visit(Overloaded{std::forward<Ts>(ts)...}, std::move(var));
36}
37
38} // namespace utils
39
40USERVER_NAMESPACE_END