When should I put a `requires` clause in a template function's signature instead of before it?

132 Views Asked by At

In C++20 you can add a requires clause (1) after a template parameter list as well as (2) after a function declaration (like a specifier). In the following snippet foo is an example of (1) and bar is an example of (2).

#include <type_traits>

template <typename T>
  requires std::is_integral_v<T>
bool foo(T x) {
  return x > 0;
}

template <typename T>
bool foo(T x) {
  return false;
}

template <typename T>
bool bar(T x)
  requires std::is_integral_v<T>
{
  return x > 0;
}

template <typename T>
bool bar(T x) {
  return false;
}

How is overload resolution going to differ between functions like foo and functions like bar? Is there any difference?

0

There are 0 best solutions below