constexpr.hpp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. ///////////////////////////////////////////////////////////////
  2. // Copyright 2019 John Maddock. Distributed under the Boost
  3. // Software License, Version 1.0. (See accompanying file
  4. // LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt
  5. #ifndef BOOST_MP_CONSTEXPR_HPP
  6. #define BOOST_MP_CONSTEXPR_HPP
  7. #include <boost/config.hpp>
  8. namespace boost {
  9. namespace multiprecision {
  10. namespace std_constexpr {
  11. template <class T>
  12. inline BOOST_CXX14_CONSTEXPR void swap(T& a, T& b)
  13. {
  14. T t(a);
  15. a = b;
  16. b = t;
  17. }
  18. template <class InputIterator, class OutputIterator>
  19. inline BOOST_CXX14_CONSTEXPR OutputIterator copy(InputIterator first, InputIterator last, OutputIterator result)
  20. {
  21. //
  22. // There are 3 branches here, only one of which is selected at compile time:
  23. //
  24. #ifndef BOOST_MP_NO_CONSTEXPR_DETECTION
  25. if (BOOST_MP_IS_CONST_EVALUATED(*first))
  26. {
  27. // constexpr safe code, never generates runtime code:
  28. while (first != last)
  29. {
  30. *result = *first;
  31. ++first;
  32. ++result;
  33. }
  34. return result;
  35. }
  36. else
  37. #endif
  38. {
  39. #ifndef BOOST_NO_CXX17_IF_CONSTEXPR
  40. if constexpr (std::is_pointer<InputIterator>::value && std::is_pointer<OutputIterator>::value && std::is_trivially_copyable<typename std::remove_reference<decltype(*first)>::type>::value)
  41. {
  42. // The normal runtime branch:
  43. std::memcpy(result, first, (last - first) * sizeof(*first));
  44. return result + (last - first);
  45. }
  46. else
  47. #endif
  48. {
  49. // Alternate runtime branch:
  50. while (first != last)
  51. {
  52. *result = *first;
  53. ++first;
  54. ++result;
  55. }
  56. return result;
  57. }
  58. }
  59. }
  60. template <class I>
  61. inline BOOST_CXX14_CONSTEXPR bool equal(const I* first, const I* last, const I* other)
  62. {
  63. while (first != last)
  64. {
  65. if (*first != *other)
  66. return false;
  67. ++first;
  68. ++other;
  69. }
  70. return true;
  71. }
  72. }
  73. }
  74. } // namespace boost::multiprecision::std_constexpr
  75. #endif