Creative operator overloading and proxy objects
As you might already know, C++ has the ability to overload several operators, including the standard math operators such as plus and minus. Overloaded math operators can be utilized to create custom math classes that behave as numeric built-in types to make the code more readable. Another example is the stream operator, which in the standard library is overloaded in order to convert the objects to streams, as shown here:
std::cout << "iostream " << "uses " << "overloaded " << "operators.";
Some libraries, however, use overloading in other contexts. The Ranges library, as discussed earlier, uses overloading to compose views like this:
const auto r = {-5, -4, -3, -2, -1, 0, 1, 2, 3, 4, 5};
auto odd_positive_numbers = r
| std::views::filter([](auto v) { return v > 0; })
| std::views::filter([](auto v) { return (v % 2) == 1; });
Next, we will explore...