-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathoverload.hpp
66 lines (56 loc) · 1.93 KB
/
overload.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
///
// TL library - A collection of small C++ utilities
// Written in 2017 by Simon Brand (@TartanLlama)
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to the
// public domain worldwide. This software is distributed without any warranty.
//
// You should have received a copy of the CC0 Public Domain Dedication
// along with this software. If not, see <http://creativecommons.org/publicdomain/zero/1.0/>.
///
// A rudimentary implementation of std::overload
// http://open-std.org/JTC1/SC22/WG21/docs/papers/2016/p0051r2.pdf
//
// This allows in-place visitation of variant types
//
// Example:
// variant<int,float,std::string> my_variant;
// auto do_something = overload(
// [](int i) { /* do something with int */ },
// [](float f) { /* do something with float */ },
// [](std::string s) { /* do something with string */ }
// );
// visit(do_something, my_variant);
///
#ifndef TL_OVERLOAD_HPP
#define TL_OVERLOAD_HPP
#include <utility>
namespace tl {
namespace detail {
template <class... Ts>
struct overloader;
template <>
struct overloader<> {};
template <class T>
struct overloader<T> : T {
using T::operator();
template <class U>
overloader(U&& u)
: T{std::forward<U>(u)} {}
};
template <class T, class... Ts>
struct overloader<T, Ts...> : T, overloader<Ts...> {
using T::operator();
using overloader<Ts...>::operator();
template <class U, class... Us>
overloader(U&& u, Us&&... us)
: T{std::forward<U>(u)}, overloader<Ts...>{std::forward<Us>(us)...} {}
};
}
template <class... Ts>
auto overload(Ts&&... ts) {
return detail::overloader<typename std::decay<Ts>::type...>{std::forward<Ts>(ts)...};
}
}
#endif