]> git.proxmox.com Git - ceph.git/blame - 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
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.hpp>
6
7#include <string>
8#include <vector>
9namespace hana = boost::hana;
10
11
12struct yes { std::string toString() const { return "yes"; } };
13struct no { };
14
15//! [optionalToString.sfinae]
16template <typename T>
17std::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
26int main() {
27BOOST_HANA_RUNTIME_CHECK(optionalToString(yes{}) == "yes");
28BOOST_HANA_RUNTIME_CHECK(optionalToString(no{}) == "toString not defined");
29
30{
31
32//! [maybe_add]
33auto maybe_add = hana::sfinae([](auto x, auto y) -> decltype(x + y) {
34 return x + y;
35});
36
37maybe_add(1, 2); // hana::just(3)
38
39std::vector<int> v;
40maybe_add(v, "foobar"); // hana::nothing
41//! [maybe_add]
42
43}
44}