1

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?

nebuch
  • 6,475
  • 4
  • 20
  • 39
  • 1
    With multiple constraints the order in which they're evaluated changes depending on where the constraints are placed lexically, but there's no difference in the simple example you have here. – ildjarn Dec 27 '22 at 18:03
  • 1
    No difference. I would put restrictions on the template parameters right after they are declared, and restrictions on the function parameters right after *they* are declared. You might have both! – BoP Dec 27 '22 at 18:04

0 Answers0