forked from BowenFu/matchit.cpp
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheval.cpp
More file actions
29 lines (26 loc) · 655 Bytes
/
eval.cpp
File metadata and controls
29 lines (26 loc) · 655 Bytes
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
#include "matchit.h"
#include <iostream>
template <typename T1, typename T2>
constexpr auto eval(std::tuple<char, T1, T2> const &expr)
{
using namespace matchit;
Id<T1> i;
Id<T2> j;
return match(expr)(
pattern | ds('+', i, j) = i + j, pattern | ds('-', i, j) = i - j,
pattern | ds('*', i, j) = i * j, pattern | ds('/', i, j) = i / j,
pattern | _ = []
{
assert(false);
return -1;
});
}
#if __cplusplus > 201703L
constexpr auto result = eval(std::make_tuple('*', 5, 6));
static_assert(result == 30);
#endif
int32_t main()
{
std::cout << eval(std::make_tuple('*', 5, 6)) << std::endl;
return 0;
}