]> git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/hana/example/tutorial/introspection.sfinae.cpp
update sources to v12.2.3
[ceph.git] / ceph / src / boost / libs / hana / example / tutorial / introspection.sfinae.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)
4
5 #include <boost/hana.hpp>
6
7 #include <string>
8 #include <vector>
9 namespace hana = boost::hana;
10
11
12 struct yes { std::string toString() const { return "yes"; } };
13 struct no { };
14
15 //! [optionalToString.sfinae]
16 template <typename T>
17 std::string optionalToString(T const& obj) {
18 auto maybe_toString = hana::sfinae([](auto&& x) -> decltype(x.toString()) {
19 return x.toString();
20 });
21
22 return maybe_toString(obj).value_or("toString not defined");
23 }
24 //! [optionalToString.sfinae]
25
26 int main() {
27 BOOST_HANA_RUNTIME_CHECK(optionalToString(yes{}) == "yes");
28 BOOST_HANA_RUNTIME_CHECK(optionalToString(no{}) == "toString not defined");
29
30 {
31
32 //! [maybe_add]
33 auto maybe_add = hana::sfinae([](auto x, auto y) -> decltype(x + y) {
34 return x + y;
35 });
36
37 maybe_add(1, 2); // hana::just(3)
38
39 std::vector<int> v;
40 maybe_add(v, "foobar"); // hana::nothing
41 //! [maybe_add]
42
43 }
44 }