2

c++20 introduces std::midpoint:

template< class T >
constexpr T midpoint( T a, T b ) noexcept;

template< class T >
constexpr T* midpoint( T* a, T* b );

Both gcc and clang make it noexcept though.

Barry
  • 286,269
  • 29
  • 621
  • 977
VainMan
  • 2,025
  • 8
  • 23
  • 1
    This is the so-called [Lakos rule](https://stackoverflow.com/questions/66603543/why-those-two-stdstring-find-functions-are-declared-as-noexcept). – 康桓瑋 Nov 09 '21 at 13:09
  • thank you, i see. Lakos rule is basically saying that `noexcept` on functions with precondictions will prevent the implementation from validating preconditions. But IMO it doesn't, because a implementation can validate preconditions and even throw exceptions in noexcept functions, which terminates the program. On the other hand, if an implementation want to throw an implementation-defined exception that can be `catch`-ed by caller, users have to write non-portable source code. – VainMan Nov 09 '21 at 13:57

0 Answers0