Lets say I have a function in C++17:
template<typename Range> void calculate(const Range& range) { /* do something */ }
And I want to specialize it for all pairs of integral types (std::pair<std::integral, std::integral>), and I want to create a separate concept IntegralRange for it: how can I achieve it?
>Solution :
Specializing the function for this purpose isn’t possible; I assume you meant overloading. You can solve this problem by providing as second overload:
template<std::integral First, std::integral Second>
void calculate(std::pair<First, Second> range) { /* do something */ }
Alternatively, if you insist on using a concept:
template <typename T>
concept integral_pair = requires (T pair) {
[]<std::integral First, std::integral Second>(std::pair<First, Second>){}(pair);
};
void calculate(integral_pair auto range) { /* do something */ }
The requires expression uses an immediately invoked lambda expression (IILE) to ensure that First and Second can be deduced from pair.