Use is_constant_evaluated whenever possible

This commit is contained in:
Krystian Stasiowski
2020-02-19 22:02:41 -05:00
parent a3cba482f9
commit 165f811334
2 changed files with 15 additions and 17 deletions

View File

@ -37,9 +37,7 @@
// Standard version // Standard version
#if __cpp_lib_is_constant_evaluated >= 201811L #if __cpp_lib_is_constant_evaluated >= 201811L
#define BOOST_STATIC_STRING_IS_CONST_EVAL std::is_constant_evaluated() #define BOOST_STATIC_STRING_IS_CONST_EVAL std::is_constant_evaluated()
#elif defined(__clang__) && BOOST_STATIC_STRING_HAS_BUILTIN(__builtin_is_constant_evaluated) #elif BOOST_STATIC_STRING_HAS_BUILTIN(__builtin_is_constant_evaluated)
// If we have clang version 9+, we can use the intrinsic
// While gcc also has this, we don't need it
#define BOOST_STATIC_STRING_IS_CONST_EVAL __builtin_is_constant_evaluated() #define BOOST_STATIC_STRING_IS_CONST_EVAL __builtin_is_constant_evaluated()
#endif #endif

View File

@ -586,15 +586,8 @@ ptr_in_range(
const T* src_last, const T* src_last,
const T* ptr) const T* ptr)
{ {
// We want to make this usable in constant expressions as much as possible
// while retaining the guarentee that the comparison has a strict total ordering.
// We also want this to be fast. Since different compilers have differing levels
// of conformance, we will settle for the best option that is available.
// We don't care about this in C++11, since this function would have
// no applications in constant expressions.
#if defined(BOOST_STATIC_STRING_CPP14) && \ #if defined(BOOST_STATIC_STRING_CPP14) && \
defined(BOOST_STATIC_STRING_NO_PTR_COMP_FUNCTIONS) defined(BOOST_STATIC_STRING_IS_CONST_EVAL)
#ifdef BOOST_STATIC_STRING_IS_CONST_EVAL
// Our second best option is to use is_constant_evaluated // Our second best option is to use is_constant_evaluated
// and a loop that checks for equality, since equality for // and a loop that checks for equality, since equality for
// pointer to object types is never unspecified in this case. // pointer to object types is never unspecified in this case.
@ -605,17 +598,24 @@ defined(BOOST_STATIC_STRING_NO_PTR_COMP_FUNCTIONS)
return true; return true;
return false; return false;
} }
#else #endif
// If library comparison functions don't work, and // We want to make this usable in constant expressions as much as possible
// we cannot use any of the above, we can use // while retaining the guarentee that the comparison has a strict total ordering.
// try builtin comparison operators instead. // We also want this to be fast. Since different compilers have differing levels
// of conformance, we will settle for the best option that is available.
// We don't care about this in C++11, since this function would have
// no applications in constant expressions.
#if defined(BOOST_STATIC_STRING_CPP14) && \
defined(BOOST_STATIC_STRING_NO_PTR_COMP_FUNCTIONS)
// If library comparison functions don't work,
// we can use try builtin comparison operators instead.
return ptr >= src_first && ptr < src_last; return ptr >= src_first && ptr < src_last;
#endif #else
#endif
// Use the library comparison functions if we can't use // Use the library comparison functions if we can't use
// is_constant_evaluated or if we don't need to. // is_constant_evaluated or if we don't need to.
return std::greater_equal<const T*>()(ptr, src_first) && return std::greater_equal<const T*>()(ptr, src_first) &&
std::less<const T*>()(ptr, src_last); std::less<const T*>()(ptr, src_last);
#endif
} }
} // detail } // detail
#endif #endif