[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] odd/concepts_smf 315b931e 1/2: Begin to use 'concept
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] odd/concepts_smf 315b931e 1/2: Begin to use 'concepts' |
Date: |
Sun, 2 Oct 2022 19:25:29 -0400 (EDT) |
branch: odd/concepts_smf
commit 315b931edb4cef0671cdeef31eb40aab9354a2a5
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Begin to use 'concepts'
This commit is much like the idea sketched here:
https://lists.nongnu.org/archive/html/lmi/2022-07/msg00108.html
It seems at least arguably useful to explain why a request to move is
fulfilled by copying.
---
currency.hpp | 13 +++++++++++++
1 file changed, 13 insertions(+)
diff --git a/currency.hpp b/currency.hpp
index d0338185..c4175abe 100644
--- a/currency.hpp
+++ b/currency.hpp
@@ -24,10 +24,14 @@
#include "config.hpp"
+#include "smf.hpp" // well_move_*<>
+
#include <cmath> // rint()
+#include <concepts>
#include <limits>
#include <ostream>
#include <stdexcept> // runtime_error
+#include <type_traits>
#include <vector>
class raw_cents {}; // Tag class.
@@ -45,6 +49,7 @@ class currency
public:
using data_type = double;
+ static_assert(std::is_arithmetic_v<data_type>);
currency() = default;
currency(currency const&) = default;
@@ -68,6 +73,12 @@ class currency
data_type m_ = {};
};
+// Becomes 'regular' below, once equality operators are defined.
+static_assert(std::semiregular<currency>);
+// Move operations deliberately not defined because there is only one
+// data member and it is of a builtin arithmetic type.
+static_assert(!well_move_assignable<currency>);
+static_assert(!well_move_constructible<currency>);
consteval currency operator""_cents(unsigned long long int cents)
{
@@ -93,6 +104,8 @@ inline bool operator<=(currency lhs, currency rhs)
inline bool operator>=(currency lhs, currency rhs)
{return !operator< (lhs, rhs);}
+static_assert(std::regular<currency>);
+
inline currency operator+(currency lhs, currency rhs)
{return currency {lhs} += rhs;}
inline currency operator-(currency lhs, currency rhs)