Use Cases for Numbers
Explore how C++20 Concepts can be used to restrict templates to appropriate numeric types, excluding unwanted types like bool and char. Understand how to define and apply a Number concept to enhance type safety and avoid errors in generic numeric operations.
We'll cover the following...
Recap
So far in this course, we’ve seen what concepts are and why they were introduced in C++20. We’ve also discussed how concepts can be used with functions, classes, and templates. We’ve explored what concepts are shipped with the standard library as well as how to write our own. In the previous chapter, we saw how to use constraints with various types of concepts.
Using Numbers
We’ve been playing with a concept called Number throughout the entire course. Let’s go over a quick reminder why:
Before C++20
Our problem is that even though we only want to accept integrals and floating-point numbers, boolean values are also accepted. They are accepted because bool is an ...