Constraints and Concepts in C++
This explaination of concepts and constraints
is very basic, just to get an idea of what concepts are in cpp.
Lets start by taking an example function:
int add(int a, int b)
{
return a + b;
}
This function adds two integers and returns the result, simple!
But, now we want add function for float
and double
type as well. For that, we can use function template in c++.
template<typename T>
T add(T a, T b)
{
return a + b;
}
Now the compiler will generate the overloads of add()
when necessary. We can do:
add(4, 5); // ok
add(4.3f, 5.8f); // ok
add(3.2, 2.3); // ok
We can also do this as well:
add(std::string("some"), std::string("thing")); // ok
But, we want to keep the add function mathematical that adds numbers only. So, we want to put a constraint on function template add() that, it should only create overloads for types int
, float
and double
. Lets name this constraint NumbersOnly
. This named set of constraints is called Concepts. In code, it looks like this:
// concept NumbersOnly
template<typename T>
concept NumbersOnly = std::is_integral_v<T> || std::is_floating_point_v<T>;
// template function add that uses NumbersOnly constraint
template<typename T>
T add(T a, T b) requires NumbersOnly<T>
{
return a + b;
}
Now, when we do this:
add(std::string("some"), std::string("thing")); // error: no matching function for call to `add(string, string)`
we get an error:
error: no matching function for call to 'add(string, string)'.
This is the use of concepts, putting constraints/requirements on templates. Every concept is a predicate and evaluated at compile-time.
Extra
An easier way to do this is:
template<typename T>
T add(T a, T b) requires std::is_arithmatic_v<T>
{
return a + b;
}
A more complex way of achieving this, only with template
and type_trait
is:
template<typename T,
typename = std::enable_if<std::is_arithmetic<T>::value>::type>
T add(T a, T b)
{
return a + b;
}
More detailed explainations can be found on:
Enjoy Reading This Article?
Here are some more articles you might like to read next: