Motivation Behind C++ Concepts
Get an overview of the motivation behind concepts.
We'll cover the following
With concepts, generic programming becomes easier, safer, and more readable.
C++ templates accept any type and the real requirements are hidden in the middle of the function and class templates. In case of a problem, they will overwhelm us with lengthy, often difficult-to-read error messages.
C++ concepts reveal the requirements at the very top of the templates. They perform compile-time checks considering the required constraints and provide much more readable error messages in case of any error.
Concepts make template programming accessible to everyone.
A Simple add()
function
So far we’ve briefly seen from a very high level what we can express with concepts. But why do we need them in the first place?
For example, let’s say that we want to write a function that adds two numbers. We want to accept both integer and floating-point numbers. What can we do?
We could accept double
, maybe even long double
, and return a value of the same type.
Get hands-on with 1200+ tech skills courses.