| 1 | /*============================================================================= |
| 2 | Copyright (c) 2017 Paul Fultz II |
| 3 | static_if.cpp |
| 4 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 5 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 6 | ==============================================================================*/ |
| 7 | /*============================================================================= |
| 8 | Copyright (c) 2016 Paul Fultz II |
| 9 | static_if.cpp |
| 10 | Distributed under the Boost Software License, Version 1.0. (See accompanying |
| 11 | file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) |
| 12 | ==============================================================================*/ |
| 13 | |
| 14 | #include "example.h" |
| 15 | |
| 16 | using namespace boost::hof; |
| 17 | |
| 18 | // static_if example taken from Baptiste Wicht: |
| 19 | // http://baptiste-wicht.com/posts/2015/07/simulate-static_if-with-c11c14.html |
| 20 | |
| 21 | template<typename T> |
| 22 | void decrement_kindof(T& value) |
| 23 | { |
| 24 | eval(first_of( |
| 25 | if_(std::is_same<std::string, T>())([&](auto id){ |
| 26 | id(value).pop_back(); |
| 27 | }), |
| 28 | [&](auto id){ |
| 29 | --id(value); |
| 30 | } |
| 31 | )); |
| 32 | } |
| 33 | |
| 34 | int main() |
| 35 | { |
| 36 | std::string s = "hello!" ; |
| 37 | decrement_kindof(value&: s); |
| 38 | assert(s == "hello" ); |
| 39 | |
| 40 | int i = 4; |
| 41 | decrement_kindof(value&: i); |
| 42 | assert(i == 3); |
| 43 | } |
| 44 | |