]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/hana/example/misc/overload_linearly.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / hana / example / misc / overload_linearly.cpp
CommitLineData
b32b8144 1// Copyright Louis Dionne 2013-2017
7c673cae
FG
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)
4
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>
12
13#include <string>
14namespace hana = boost::hana;
15
16
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.
20
21auto 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...);
25 });
26 auto result = hana::transform(maybe_function, [=](auto f) {
27 return f(args...);
28 });
29 return result;
30 };
31};
32
33int main() {
34 auto f = ::overload_linearly(
35 [](std::string s) { return s + "abcd"; },
36 [](int i) { return i + 1; },
37 [](double f) { return f + 2; }
38 );
39
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));
42}