-
Notifications
You must be signed in to change notification settings - Fork 1
/
example.cpp
35 lines (30 loc) · 881 Bytes
/
example.cpp
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
#include "tyre/tyre.h"
#include <cassert>
#include <iostream>
struct duck_visitors
{
static constexpr tyre::visitor_list visitors
{
tyre::visitor<struct look, bool(std::any const&)>([](auto const& x) { return x.look(); }),
tyre::visitor<struct walk, void(std::any&, int)>([](auto& x, int n) { x.walk(n); }),
tyre::visitor<struct quack, void(std::any&)>([](auto& x) { x.quack(); }),
};
};
using any_duck = tyre::any<duck_visitors>;
void test(any_duck duck)
{
bool const ok = tyre::visit<look>(duck);
tyre::visit<walk>(duck, 42);
tyre::visit<quack>(duck);
assert(ok);
}
int main()
{
struct my_duck
{
bool look() const { std::cout << "o_O\n"; return true; }
void walk(int n) { std::cout << "Walked " << n << " steps.\n"; }
void quack() { std::cout << "Quack!\n"; }
};
test(my_duck());
}