Concluding Concepts
Conclusion of Concepts.
🔑 Concepts in C++20: an evolution or a revolution?
This small detour expresses my opinion. First, I present the facts, then I draw my conclusion. The facts are based on what has been presented in this chapter. So which arguments speak for evolution or for revolution?
Evolution
- Concepts promote working with generic code at a higher level of abstraction.
- Concepts give you understandable error messages when compiling a template fails. They provide nothing you could not achieve with the type-traits library, SFINAE, and static_assert.
auto
is a kind of unconstrained placeholder. With C++20, we can use concepts as constrained placeholders.- With C++14, we could use generic lambdas as a convenient way to define function templates.
Revolution
- Concepts allow us, for the first time, to verify template requirements. Of course, you can also achieve the verification of template parameters with a combination of type-traits library, SFINAE, and static_assert, but this technique is way too advanced to regard it as a general solution.
- Thanks to the abbreviated function templates syntax, defining templates has been radically improved.
- Concepts represent semantic categories, but not syntactic constraints. Instead of a concept such as
Addable
, which requires that a type supports the+
operator, we should think in terms of a conceptNumber
.Number
is a semantic category such asEqual
orOrdering
.My conclusion
There are many arguments about whether concepts are an evolutionary step or a revolutionary jump. Mainly because of the semantic categories, I’m on the revolution side. Concepts such as
Number
,Equality
, orOrdering
remind me of Plato’s world of ideas. It is revolutionary that we can now reason about programming in such categories.
Get hands-on with 1400+ tech skills courses.