[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[lmi-commits] [lmi] master b538769 6/8: Use std::endian when using C++20
From: |
Greg Chicares |
Subject: |
[lmi-commits] [lmi] master b538769 6/8: Use std::endian when using C++20 |
Date: |
Mon, 19 Apr 2021 20:37:30 -0400 (EDT) |
branch: master
commit b538769eccbdf7819444801ef6c7b481cde51211
Author: Vadim Zeitlin <vadim@tt-solutions.com>
Commit: Gregory W. Chicares <gchicares@sbcglobal.net>
Use std::endian when using C++20
Also check for the value of __cplusplus corresponding to C++20.
This fixes compilation with C++20 compilers.
---
actuarial_table.cpp | 9 +++++----
rate_table.cpp | 20 ++++++++++++++------
2 files changed, 19 insertions(+), 10 deletions(-)
diff --git a/actuarial_table.cpp b/actuarial_table.cpp
index 6a9ab14..e9edad5 100644
--- a/actuarial_table.cpp
+++ b/actuarial_table.cpp
@@ -37,12 +37,14 @@
#include <boost/filesystem/path.hpp>
#include <algorithm> // max(), min()
+#if 202002 <= __cplusplus
+# include <bit> // endian
+#endif // 202002 <= __cplusplus
#include <cctype> // toupper()
#include <cstdint>
#include <ios>
#include <istream>
#include <limits>
-#include <type_traits> // endian
namespace
{
@@ -92,10 +94,9 @@ actuarial_table::actuarial_table(std::string const&
filename, int table_number)
{
// Binary tables in the SOA format are not portable; this code
// presumably works only on little-endian hardware.
-#if 201900L < __cplusplus
- #error Use the proper C++20 value, which was unknown when this was written.
+#if 202002 <= __cplusplus
static_assert(std::endian::native == std::endian::little);
-#endif // 201900L < __cplusplus
+#endif // 202002 <= __cplusplus
if(table_number_ <= 0)
{
diff --git a/rate_table.cpp b/rate_table.cpp
index 82ab99a..93260fa 100644
--- a/rate_table.cpp
+++ b/rate_table.cpp
@@ -35,6 +35,9 @@
#include <boost/filesystem/fstream.hpp>
#include <algorithm> // count()
+#if 202002 <= __cplusplus
+# include <bit> // endian
+#endif // 202002 <= __cplusplus
#include <climits> // ULLONG_MAX
#include <cstdlib> // strtoull()
#include <cstring> // memcpy(), strncmp()
@@ -68,9 +71,17 @@
// different from their in memory representation.
static_assert(std::numeric_limits<double>::is_iec559);
-#if 201900L < __cplusplus
- #error Use std::endian instead when it becomes available.
-#endif // 201900L < __cplusplus
+// Check that WORDS_BIGENDIAN, which is supposed to be defined in makefile/at
+// the compiler command line if necessary, is consistent with the actual
+// endianness used.
+#if 202002 <= __cplusplus
+# if defined WORDS_BIGENDIAN
+static_assert(std::endian::native == std::endian::big);
+# else // !defined WORDS_BIGENDIAN
+static_assert(std::endian::native == std::endian::little);
+# endif // !defined WORDS_BIGENDIAN
+#endif // !(202002 <= __cplusplus)
+
// Helper functions used to swap bytes on big endian platforms.
namespace
{
@@ -83,9 +94,6 @@ std::uint8_t swap_bytes_if_big_endian(std::uint8_t val)
return val;
}
-// We rely on makefile defining WORDS_BIGENDIAN on big endian architectures,
-// conversions from little endian format are only needed there and are trivial
-// on little endian machines.
#if defined WORDS_BIGENDIAN
inline
std::uint16_t swap_bytes_if_big_endian(std::uint16_t val)
- [lmi-commits] [lmi] master updated (9414edf -> 5aa44f4), Greg Chicares, 2021/04/19
- [lmi-commits] [lmi] master d9e35c2 2/8: Remove an unimportant comment, Greg Chicares, 2021/04/19
- [lmi-commits] [lmi] master 7f8f3e0 1/8: Fix defect introduced 20210418T2056Z: nonzero default value, Greg Chicares, 2021/04/19
- [lmi-commits] [lmi] master 85076e0 7/8: Disable -Wunused-parameter warning in Boost.Numeric header, Greg Chicares, 2021/04/19
- [lmi-commits] [lmi] master a71c8cd 4/8: Use wxSizerFlags for creating the "About" dialog, Greg Chicares, 2021/04/19
- [lmi-commits] [lmi] master 3d3f40e 3/8: Update coding-rule exemptions, Greg Chicares, 2021/04/19
- [lmi-commits] [lmi] master 57f608a 5/8: Disable clang enum-related warnings in C++20 mode, Greg Chicares, 2021/04/19
- [lmi-commits] [lmi] master b538769 6/8: Use std::endian when using C++20,
Greg Chicares <=
- [lmi-commits] [lmi] master 5aa44f4 8/8: Disable clang warnings about using volatile in C++20 mode, Greg Chicares, 2021/04/19