In line 3, a vector
has been defined and initialized. In line 4, the print_range
function has been called to print the vector
. The output is as desired. However, when lines 6–7 are uncommented, an error is generated at compile-time, indicating that no matching function exists for the int
array because it doesn't have begin
and end
functions. This saves any runtime error that may have caused the termination of an execution.
Conclusion#
In short, concept
provides a more readable and maintainable way to define constraints on template parameters. It makes it easier to express the requirements of a template argument and to write generic code that works with a wide range of types.
We hope that this blog has not only triggered your quest to learn more about C++ 20 but also inspired you to learn to code in C++ with greater depth. For further readings please continue with the following courses:
C++ Concepts: Improve Type Safety with C++ 20 is all about using concept
for type safety.
The All-in-One Guide to C++20 introduces C++20 in great detail.
C++ Fundamentals for Professionals is for refreshing C++.