From 93d2ad6bd0161ee577b49de463b5e1d65ea6f92e Mon Sep 17 00:00:00 2001 From: King_DuckZ Date: Thu, 22 Apr 2021 13:02:51 +0200 Subject: [PATCH] Make ary_to_int() constexpr --- CMakeLists.txt | 2 +- include/duckhandy/implem/int_conv.hpp | 22 +++++++++++++--------- 2 files changed, 14 insertions(+), 10 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 43607b7..0621e5e 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -27,7 +27,7 @@ target_compile_features(${PROJECT_NAME} INTERFACE cxx_decltype_incomplete_return_types INTERFACE cxx_noexcept INTERFACE cxx_rvalue_references - INTERFACE cxx_std_17 + INTERFACE cxx_std_20 ) target_include_directories(${PROJECT_NAME} diff --git a/include/duckhandy/implem/int_conv.hpp b/include/duckhandy/implem/int_conv.hpp index db9aa20..9d941c6 100644 --- a/include/duckhandy/implem/int_conv.hpp +++ b/include/duckhandy/implem/int_conv.hpp @@ -33,6 +33,7 @@ # include # include #endif +#include namespace dhandy { namespace implem { @@ -168,15 +169,15 @@ namespace dhandy { template [[gnu::always_inline,gnu::pure]] - inline T negated_ifn (T n, bool negate) { + constexpr inline T negated_ifn (T n, bool negate) { //return static_cast(((static_cast(n) - (mask bitand 1)) xor mask) bitor ((mask bitand 1) << 31)); return (negate ? -n : n); } - template + template struct AryConversion { template - static I from_ary (const C* beg, const C* end) { + constexpr static I from_ary (const C* beg, const C* end) { I retval = 0; I factor = 1; std::size_t i = end - beg; @@ -195,13 +196,13 @@ namespace dhandy { }; template - struct AryConversion::value and not std::is_same::value and sizeof(I) <= sizeof(uint32_t)>::type> { + struct AryConversion::value and not std::is_same::value and sizeof(I) <= sizeof(uint32_t)>::type> { template static I from_ary (C* beg, C* end) { return to_integer_sse(beg, end - beg); } }; - template - struct AryConversion { - template static bool from_ary (C* beg, C* end) { + template + struct AryConversion { + template constexpr static bool from_ary (C* beg, C* end) { if (end == beg) return false; return (Tr::from_digit(*beg) ? true : false); @@ -291,8 +292,11 @@ namespace dhandy { } template > - inline R ary_to_int (const C* beg, const C* end) { - return implem::AryConversion::from_ary(beg, end); + constexpr inline R ary_to_int (const C* beg, const C* end) { + if (std::is_constant_evaluated()) + return implem::AryConversion::from_ary(beg, end); + else + return implem::AryConversion::from_ary(beg, end); } #if !defined(INT_CONV_WITHOUT_HELPERS)