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//! Helper template for comparisons. Derive from this to provide !=,>,<=,>=, given == and <.
7/*!
8If you have a class, C, derive publically from this, with T=C.
9Then, just implement '==' and '<' operators, to magically get all of these.
10
11class C : public qip::Comparison<C> {
12 //...
13 friend bool operator==(const C &lhs, const C &rhs);
14 friend bool operator<(const C &lhs, const C &rhs);
15};
16
17Can have different types for LHS and RHS, e.g.:
18
19class D : public qip::Comparison<D>,
20 qip::Comparison<D,C>,
21 qip::Comparison<C,D> {
22 //...
23 friend bool operator==(const D &lhs, const D &rhs);
24 friend bool operator<(const D &lhs, const D &rhs);
25 friend bool operator==(const D &lhs, const C &rhs);
26 friend bool operator<(const D &lhs, const C &rhs);
27 friend bool operator==(const C &lhs, const D &rhs);
28 friend bool operator<(const C &lhs, const D &rhs);
29};
30*/
31template <typename T, typename U = T>
33 friend bool operator!=(const T &lhs, const U &rhs) { return !(lhs == rhs); }
34 friend bool operator>(const T &lhs, const U &rhs) { return rhs < lhs; }
35 friend bool operator<=(const T &lhs, const U &rhs) { return !(lhs > rhs); }
36 friend bool operator>=(const T &lhs, const U &rhs) { return !(lhs < rhs); }
37};
38
39//! Helper template for Arithmetic operations. Derive from this to provide +,-,*,/, given +=, -=, *=, /=
40template <typename T>
42 friend T operator+(T lhs, const T &rhs) { return lhs += rhs; }
43 friend T operator-(T lhs, const T &rhs) { return lhs -= rhs; }
44 friend T operator*(T lhs, const T &rhs) { return lhs *= rhs; }
45 friend T operator/(T lhs, const T &rhs) { return lhs /= rhs; }
46};
47
48//! Helper template for Arithmetic operations. Derive from this to provide +,-,*,/, given +=, -=, *=, /=. Works for two different types
49template <typename T, typename U>
51 friend T operator+(T lhs, const U &rhs) { return lhs += rhs; }
52 friend T operator-(T lhs, const U &rhs) { return lhs -= rhs; }
53 friend T operator*(T lhs, const U &rhs) { return lhs *= rhs; }
54 friend T operator/(T lhs, const U &rhs) { return lhs /= rhs; }
55 // Also define these symmetric ones
56 friend T operator+(const U &lhs, T rhs) { return rhs += lhs; }
57 friend T operator*(const U &lhs, T rhs) { return rhs *= lhs; }
58};
59
60} // namespace qip
Helper template for Arithmetic operations. Derive from this to provide +,-,*,/, given +=,...
Definition Template.hpp:50
Helper template for Arithmetic operations. Derive from this to provide +,-,*,/, given +=,...
Definition Template.hpp:41
Helper template for comparisons. Derive from this to provide !=,>,<=,>=, given == and <.
Definition Template.hpp:32
qip library: A collection of useful functions
Definition Array.hpp:9