[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] odd/c02 4fb7493 2/2: Make operator""_cents consteval
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] odd/c02 4fb7493 2/2: Make operator""_cents consteval |
Date: |
Sun, 9 May 2021 10:20:56 -0400 (EDT) |
branch: odd/c02
commit 4fb7493583b827489839b71315c75fc0e060876d
Author: Gregory W. Chicares <gchicares@sbcglobal.net>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Make operator""_cents consteval
See:
https://lists.nongnu.org/archive/html/lmi/2021-05/msg00003.html
---
currency.hpp | 4 ++--
currency_test.cpp | 12 +++++++++++-
2 files changed, 13 insertions(+), 3 deletions(-)
diff --git a/currency.hpp b/currency.hpp
index ecfb177..da79f59 100644
--- a/currency.hpp
+++ b/currency.hpp
@@ -39,7 +39,7 @@ class currency
friend currency from_cents(double); // private ctor
template<typename> friend class round_to; // private ctor
friend class round_to_test; // currency::cents_digits
- friend constexpr currency operator""_cents(unsigned long long int);
+ friend consteval currency operator""_cents(unsigned long long int);
static constexpr int cents_digits = 2;
static constexpr double cents_per_dollar = 100.0;
@@ -70,7 +70,7 @@ class currency
data_type m_ = {};
};
-inline constexpr currency operator""_cents(unsigned long long int cents)
+inline consteval currency operator""_cents(unsigned long long int cents)
{
constexpr auto mant_dig = std::numeric_limits<currency::data_type>::digits;
constexpr unsigned long long int limit = 1ULL << mant_dig;
diff --git a/currency_test.cpp b/currency_test.cpp
index c0f08f2..7c2a63c 100644
--- a/currency_test.cpp
+++ b/currency_test.cpp
@@ -163,6 +163,7 @@ void currency_test::test_literals()
currency const nc9007199254740992( -9007199254740992_cents);
LMI_TEST_EQUAL( -9007199254740992, nc9007199254740992.m_);
+#if 0
// These are run-time errors:
LMI_TEST_THROW
(9007199254740993_cents
@@ -176,7 +177,7 @@ void currency_test::test_literals()
,std::runtime_error
,"currency: 9007199254740993 out of bounds"
);
-
+#endif // 0
// These are evaluated at compile time:
constexpr currency compile_time_constant_pos( 9007199254740992_cents);
constexpr currency compile_time_constant_neg(-9007199254740992_cents);
@@ -184,9 +185,18 @@ void currency_test::test_literals()
// constexpr currency error_at_compile_time_pos( 9007199254740993_cents);
// constexpr currency error_at_compile_time_neg(-9007199254740993_cents);
}
+#include <array>
+
+ consteval currency f() {return C0;}
+ consteval currency g() {return 10_cents;}
+
+ std::array<int, static_cast<int>( C0.cents())> arr0;
+ std::array<int, static_cast<int>(10_cents .cents())> arr1;
+ std::array<int, static_cast<int>(operator""_cents(10).cents())> arr2;
void currency_test::test_negation()
{
+ std::cout << arr2[0] << std::endl;
currency const a1(321, raw_cents{});
-a1;
// make sure that didn't mutate the object