-
Notifications
You must be signed in to change notification settings - Fork 1
/
visitor.cpp
91 lines (63 loc) · 1.9 KB
/
visitor.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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
//
// Copyright (c) 2022-present DeepGrace (complex dot invoke at gmail dot com)
//
// 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)
//
// Official repository: https://github.com/deepgrace/smp
//
// g++ -I include -m64 -std=c++23 -s -Wall -O3 -o /tmp/visitor example/visitor.cpp
#include <string>
#include <cassert>
#include <visitor.hpp>
// access structure elements by member types
struct X
{
float f;
std::string s;
};
struct Y
{
int i;
double d;
char c;
X x;
};
int main(int argc, char* argv[])
{
Y y { 2022, 11.03, '|', { 15.9, "template" } };
// member types can be less then fields
using type = smp::fuple<int, double, char, X>;
smp::visitor<type> v;
static_assert(v.size() == 4);
static_assert(smp::fuple_size_v<type> == 4);
static_assert(std::is_same_v<smp::fuple_element_t<0, type>, int>);
static_assert(std::is_same_v<smp::fuple_element_t<2, type>, char>);
// extract the nth member by reference
v.get<0>(y) = 2023;
v.get<3>(y).s = "Template";
assert(y.i == 2023);
assert(y.x.s == "Template");
// turn to smp::fuple or std::tuple by value
auto f = v.get<1, 0>(y);
auto t = v.get<0, 0>(y);
y.d = 4.28;
assert(v.get<1>(y) == 4.28);
assert(y.d != smp::get<1>(f));
assert(y.d != std::get<1>(t));
assert(smp::get<1>(f) == std::get<1>(t));
// extract the nth member by reference
smp::get<2>(f) = 'F';
std::get<2>(t) = 'T';
assert(smp::get<2>(f) == 'F');
assert(std::get<2>(t) == 'T');
assert(y.c == '|');
// turn to smp::fuple or std::tuple by reference
auto rf = v.get<1, 1>(y);
auto rt = v.get<0, 1>(y);
smp::get<3>(rf).f = 11.8f;
std::get<3>(rt).s = "MetaProgramming";
assert(y.x.f == 11.8f);
assert(y.x.s == "MetaProgramming");
return 0;
}