]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/hana/example/misc/overload_linearly.cpp
1 // Copyright Louis Dionne 2013-2017
2 // Distributed under the Boost Software License, Version 1.0.
3 // (See accompanying file LICENSE.md or copy at http://boost.org/LICENSE_1_0.txt)
5 #include <boost/hana/assert.hpp>
6 #include <boost/hana/equal.hpp>
7 #include <boost/hana/find_if.hpp>
8 #include <boost/hana/optional.hpp>
9 #include <boost/hana/transform.hpp>
10 #include <boost/hana/tuple.hpp>
11 #include <boost/hana/type.hpp>
14 namespace hana
= boost::hana
;
17 // We have an utility in the Functional module that does pretty much the
18 // same, but is more compile-time efficient. It is still interesting to
19 // see this implemented with sequences and the SFINAE combinator.
21 auto overload_linearly
= [](auto ...candidates
) {
22 return [=](auto ...args
) {
23 auto maybe_function
= hana::find_if(hana::make_tuple(candidates
...), [=](auto f
) {
24 return hana::is_valid(f
)(args
...);
26 auto result
= hana::transform(maybe_function
, [=](auto f
) {
34 auto f
= ::overload_linearly(
35 [](std::string s
) { return s
+ "abcd"; },
36 [](int i
) { return i
+ 1; },
37 [](double f
) { return f
+ 2; }
40 BOOST_HANA_RUNTIME_CHECK(f(1) == hana::just(1 + 1));
41 BOOST_HANA_RUNTIME_CHECK(f(2.3) == hana::just(static_cast<int>(2.3) + 1));