summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/hof/example/static_if.cpp
blob: 898e66213718c66285e4a97b67a0ec0885f76878 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/*=============================================================================
    Copyright (c) 2017 Paul Fultz II
    static_if.cpp
    Distributed under the Boost Software License, Version 1.0. (See accompanying
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/
/*=============================================================================
    Copyright (c) 2016 Paul Fultz II
    static_if.cpp
    Distributed under the Boost Software License, Version 1.0. (See accompanying
    file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
==============================================================================*/

#include "example.h"

using namespace boost::hof;

// static_if example taken from Baptiste Wicht:
// http://baptiste-wicht.com/posts/2015/07/simulate-static_if-with-c11c14.html

template<typename T>
void decrement_kindof(T& value)
{
    eval(first_of(
        if_(std::is_same<std::string, T>())([&](auto id){
            id(value).pop_back();
        }),
        [&](auto id){
            --id(value);
        }
    ));
}

int main()
{
    std::string s = "hello!";
    decrement_kindof(s);
    assert(s == "hello");

    int i = 4;
    decrement_kindof(i);
    assert(i == 3);
}