Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Concept for a type that checks if a class is one specified template class, which is parametrized by types, that satisfy another concept

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?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading