ampsci
High-precision calculations for one- and two-valence atomic systems
Template.hpp
1#pragma once
2#include <type_traits>
3
4namespace qip {
5
6/*!
7 @brief Helper template that provides !=, >, <=, >= given == and <.
8
9 @details
10 Derive publicly from this (with T = your class) and implement == and <;
11 the remaining comparison operators are provided automatically.
12
13 @code{.cpp}
14 class C : public qip::Comparison<C> {
15 friend bool operator==(const C &lhs, const C &rhs);
16 friend bool operator<(const C &lhs, const C &rhs);
17 };
18 @endcode
19
20 For mixed-type comparisons, inherit multiple specialisations:
21
22 @code{.cpp}
23 class D : public qip::Comparison<D>,
24 qip::Comparison<D,C>,
25 qip::Comparison<C,D> {
26 friend bool operator==(const D &lhs, const D &rhs);
27 friend bool operator<(const D &lhs, const D &rhs);
28 friend bool operator==(const D &lhs, const C &rhs);
29 friend bool operator<(const D &lhs, const C &rhs);
30 friend bool operator==(const C &lhs, const D &rhs);
31 friend bool operator<(const C &lhs, const D &rhs);
32 };
33 @endcode
34*/
35template <typename T, typename U = T>
37 friend bool operator!=(const T &lhs, const U &rhs) { return !(lhs == rhs); }
38 friend bool operator>(const T &lhs, const U &rhs) { return rhs < lhs; }
39 friend bool operator<=(const T &lhs, const U &rhs) { return !(lhs > rhs); }
40 friend bool operator>=(const T &lhs, const U &rhs) { return !(lhs < rhs); }
41};
42
43/*!
44 @brief Helper template that provides +, -, *, / given +=, -=, *=, /=.
45
46 @details
47 Derive publicly from this (with T = your class) and implement the
48 compound-assignment operators; the binary operators are provided automatically.
49*/
50template <typename T>
52 friend T operator+(T lhs, const T &rhs) { return lhs += rhs; }
53 friend T operator-(T lhs, const T &rhs) { return lhs -= rhs; }
54 friend T operator*(T lhs, const T &rhs) { return lhs *= rhs; }
55 friend T operator/(T lhs, const T &rhs) { return lhs /= rhs; }
56};
57
58/*!
59 @brief Like @ref Arithmetic, but for two different types (T op U).
60
61 @details
62 Provides T+U, T-U, T*U, T/U (and the symmetric U+T, U*T) given the
63 compound-assignment operators on T.
64*/
65template <typename T, typename U>
67 friend T operator+(T lhs, const U &rhs) { return lhs += rhs; }
68 friend T operator-(T lhs, const U &rhs) { return lhs -= rhs; }
69 friend T operator*(T lhs, const U &rhs) { return lhs *= rhs; }
70 friend T operator/(T lhs, const U &rhs) { return lhs /= rhs; }
71 // Also define these symmetric ones
72 friend T operator+(const U &lhs, T rhs) { return rhs += lhs; }
73 friend T operator*(const U &lhs, T rhs) { return rhs *= lhs; }
74};
75
76} // namespace qip
Like Arithmetic, but for two different types (T op U).
Definition Template.hpp:66
Helper template that provides +, -, *, / given +=, -=, *=, /=.
Definition Template.hpp:51
Helper template that provides !=, >, <=, >= given == and <.
Definition Template.hpp:36
General-purpose utility library.
Definition Array.hpp:23