pointer_in_range.hpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. /*
  2. Copyright 2024 Glen Joseph Fernandes
  3. (glenjofe@gmail.com)
  4. Distributed under the Boost Software License, Version 1.0.
  5. (http://www.boost.org/LICENSE_1_0.txt)
  6. */
  7. #ifndef BOOST_CORE_POINTER_IN_RANGE_HPP
  8. #define BOOST_CORE_POINTER_IN_RANGE_HPP
  9. #include <boost/config.hpp>
  10. #include <functional>
  11. #if !defined(BOOST_NO_CXX14_CONSTEXPR)
  12. #if defined(BOOST_MSVC) && BOOST_MSVC >= 1925
  13. #define BOOST_CORE_DETAIL_HAS_IS_CONSTEVAL
  14. #elif defined(__has_builtin)
  15. #if __has_builtin(__builtin_is_constant_evaluated)
  16. #define BOOST_CORE_DETAIL_HAS_IS_CONSTEVAL
  17. #endif
  18. #endif
  19. #endif
  20. #if !defined(BOOST_CORE_DETAIL_HAS_IS_CONSTEVAL)
  21. #define BOOST_CORE_NO_CONSTEXPR_POINTER_IN_RANGE
  22. #endif
  23. namespace boost {
  24. template<class T>
  25. inline BOOST_CONSTEXPR bool
  26. pointer_in_range(const T* p, const T* b, const T* e)
  27. {
  28. #if defined(BOOST_CORE_DETAIL_HAS_IS_CONSTEVAL)
  29. if ( __builtin_is_constant_evaluated()) {
  30. for (; b != e; ++b) {
  31. if (b == p) {
  32. return true;
  33. }
  34. }
  35. return false;
  36. }
  37. #endif
  38. return std::less_equal<const T*>()(b, p) && std::less<const T*>()(p, e);
  39. }
  40. } /* boost */
  41. #endif