How to Choose Among the Four Ways?
Let’s see how to choose among the 4 ways of using concepts.
We'll cover the following...
We’ve just seen four ways to use concepts. Let’s have a look at them together:
- The
requires
clause - The trailing
requires
clause - The constrained template parameter
- The abbreviated function templates
Press + to interact
#include <concepts>#include <iostream>//creating a concept 'Number'template <typename T>concept Number = std::integral<T> || std::floating_point<T>;//using requires clausetemplate <typename T>requires Number<T>auto addRequiresClause(T a, T b) {return a+b;}//using trailing requires clausetemplate <typename T>auto addTrailingRequiresClause(T a, T b) requires Number<T> {return a+b;}//using constrained template parametertemplate <Number T>auto addConstrainedTemplate(T a, T b) {return a+b;}//using abbreviated function templatesauto addAbbreviatedFunctionTemplate(Number auto a, Number auto b) {return a+b;}int main() {std::cout << "addRequiresClause(1, 2): " << addRequiresClause(1, 2) << '\n';// std::cout << "addRequiresClause(1, 2.5): "// << addRequiresClause(1, 2.5) << '\n';// error: no matching function for call to 'addRequiresClause(int, double)'std::cout << "addTrailingRequiresClause(1, 2): "<< addTrailingRequiresClause(1, 2) << '\n';// std::cout << "addTrailinRequiresClause(1, 2): "// << addTrailinRequiresClause(1, 2.5) << '\n';// error: no matching function for call to// 'addTrailinRequiresClause(int, double)'std::cout << "addConstrainedTemplate(1, 2): "<< addConstrainedTemplate(1, 2) << '\n';// std::cout << "addConstrainedTemplate(1, 2): "// << addConstrainedTemplate(1, 2.5) << '\n';// error: no matching function for call to 'addConstrainedTemplate(int, double)'std::cout << "addAbbreviatedFunctionTemplate(1, 2): "<< addAbbreviatedFunctionTemplate(1, 2) << '\n';std::cout << "addAbbreviatedFunctionTemplate(1, 2): "<< addAbbreviatedFunctionTemplate(1, 2.14) << '\n';}
Which method should we use? As always, it ...