comparison templates/named_operator.hpp @ 0:a4671277546c tip

created the repository for the thymian project
author ferencd
date Tue, 17 Aug 2021 11:19:54 +0200
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:a4671277546c
1 #ifndef BASE_NAMED_OPERATOR_HPP
2 #define BASE_NAMED_OPERATOR_HPP
3
4 #include <utility>
5
6 namespace base {
7
8 template <typename F>
9 struct named_operator_wrapper {
10 F f;
11 };
12
13 template <typename T, typename F>
14 struct named_operator_lhs {
15 F f;
16 T& value;
17 };
18
19 template <typename T, typename F>
20 inline named_operator_lhs<T, F> operator <(T& lhs, named_operator_wrapper<F> rhs) {
21 return {rhs.f, lhs};
22 }
23
24 template <typename T, typename F>
25 inline named_operator_lhs<T const, F> operator <(T const& lhs, named_operator_wrapper<F> rhs) {
26 return {rhs.f, lhs};
27 }
28
29 template <typename T1, typename T2, typename F>
30 inline auto operator >(named_operator_lhs<T1, F> const& lhs, T2 const& rhs)
31 -> decltype(lhs.f(std::declval<T1>(), std::declval<T2>()))
32 {
33 return lhs.f(lhs.value, rhs);
34 }
35
36 template <typename T1, typename T2, typename F>
37 inline auto operator >=(named_operator_lhs<T1, F> const& lhs, T2 const& rhs)
38 -> decltype(lhs.value = lhs.f(std::declval<T1>(), std::declval<T2>()))
39 {
40 return lhs.value = lhs.f(lhs.value, rhs);
41 }
42
43 template <typename F>
44 inline
45 #ifndef _MSC_VER
46 constexpr
47 #endif
48 named_operator_wrapper<F> make_named_operator(F f) {
49 return {f};
50 }
51
52 } // namespace base
53
54 #endif // ndef BASE_NAMED_OPERATOR_HPP