Read-only mirrors. Development, issues and pull requests are on GitHub.

summaryrefslogtreecommitdiffhomepage
path: root/src/Progmasoft/Catch3/Random.hpp
diff options
context:
space:
mode:
authorLeitwolf112026-10-31 13:00:24 +0300
committerLeitwolf112026-10-31 13:00:24 +0300
commit8814bd0b796b2e291fa5b4a1033f0313c8a26065 (patch)
treee87e5f5150f3712bb05ed53911425600aa871f45 /src/Progmasoft/Catch3/Random.hpp
parent6a42cdf6461b2577106ef094d6f98025554b6077 (diff)
downloadcatch3-8814bd0b796b2e291fa5b4a1033f0313c8a26065.tar.gz
Build Catch3 as C++17
C++17 is still the widest common target, and a library built on it stays usable from C++17 and every later standard, while C++20 would have left out many users. Concepts become enable_if on named traits in Detail/TypeTraits.hpp, which also replaces std::remove_cvref_t and std::identity. std::span becomes std::vector references, std::bit_cast becomes static_cast and memcpy, template lambdas become helper templates, and std::erase_if, contains, starts_with and ends_with have their C++17 forms. Random::Between no longer uses a lambda over a constexpr local, which MSVC rejects outside /permissive-, so the headers work under its default mode too. Tests give up designated initializers, which MSVC refuses in C++17. CI builds the matrix in C++17 and C++20, since the headers promise both.
Diffstat (limited to 'src/Progmasoft/Catch3/Random.hpp')
-rw-r--r--src/Progmasoft/Catch3/Random.hpp43
1 files changed, 13 insertions, 30 deletions
diff --git a/src/Progmasoft/Catch3/Random.hpp b/src/Progmasoft/Catch3/Random.hpp
index a1f1ac94..31e9dd1d 100644
--- a/src/Progmasoft/Catch3/Random.hpp
+++ b/src/Progmasoft/Catch3/Random.hpp
@@ -3,8 +3,7 @@
#pragma once
#include <Progmasoft/Catch3/Detail/InvalidArgument.hpp>
-#include <bit>
-#include <concepts>
+#include <Progmasoft/Catch3/Detail/TypeTraits.hpp>
#include <cstdint>
#include <limits>
#include <stdexcept>
@@ -63,8 +62,7 @@ namespace Progmasoft::Catch3
/// Returns an inclusive integral value without overflowing at type
/// limits.
- template<std::integral IntegerType>
- requires(!std::same_as<IntegerType, bool>)
+ template<typename IntegerType, Detail::EnableIfInteger<IntegerType> = 0>
[[nodiscard]] IntegerType
Between(IntegerType minimum, IntegerType maximum)
{
@@ -82,31 +80,15 @@ namespace Progmasoft::Catch3
constexpr UnsignedType kSignBit = UnsignedType{ 1 }
<< (kBitCount - 1U);
- const auto encode
- = [](IntegerType value) constexpr -> UnsignedType {
- if constexpr (std::is_signed_v<IntegerType>)
- {
- return std::bit_cast<UnsignedType>(value) ^ kSignBit;
- }
- else
- {
- return value;
- }
- };
- const auto decode
- = [](UnsignedType value) constexpr -> IntegerType {
- if constexpr (std::is_signed_v<IntegerType>)
- {
- return std::bit_cast<IntegerType>(value ^ kSignBit);
- }
- else
- {
- return value;
- }
- };
-
- const UnsignedType lower = encode(minimum);
- const UnsignedType upper = encode(maximum);
+ // Flipping the sign bit orders signed values as unsigned ones.
+ const UnsignedType flip
+ = std::is_signed_v<IntegerType> ? kSignBit : UnsignedType{};
+ const auto lower
+ = static_cast<UnsignedType>(static_cast<UnsignedType>(minimum)
+ ^ flip);
+ const auto upper
+ = static_cast<UnsignedType>(static_cast<UnsignedType>(maximum)
+ ^ flip);
// Unsigned wrap to zero represents the complete value domain.
const UnsignedType width
= static_cast<UnsignedType>(upper - lower + UnsignedType{ 1 });
@@ -114,7 +96,8 @@ namespace Progmasoft::Catch3
= width == 0 ? static_cast<UnsignedType>(Next())
: static_cast<UnsignedType>(
Below(static_cast<std::uint64_t>(width)));
- return decode(static_cast<UnsignedType>(lower + offset));
+ return static_cast<IntegerType>(
+ static_cast<UnsignedType>(lower + offset) ^ flip);
}
/// Derives an independent, reproducible seed for one trial.