Previous section   Next section

Imperfect C++ Practical Solutions for Real-Life Programming
By Matthew Wilson
Table of Contents


Part Five: Operators

One of the most powerful and important aspects of C++ is the ability to overload operators. It facilitates the extension of the language to user-defined types, enabling them to exhibit natural syntax and to be manipulable along with built-in types in generic code. Even before templates became a part of the language, the generalized approach afforded by operator overloading was invaluable. Now that we have templates, the possibilities are almost endless.

However, operator overloading has a dark side, which stems from several factors. Some programmers nonchalantly use overloads that contradict expected semantics.[1] Another problem is the fact that C++ is still living a dual life as a super-C, carrying with it all those implicit conversion rules that we've come to live with and depend on for the fundamental types, but that can cause all kinds of problems when combined with overloaded operators for user-defined types. Finally, some of the overloadable operators have subtly different semantics to their built-in equivalents that cannot be overcome.

[1] See Appendix B.

In previous parts we've talked a little about operators, but not gone into much depth on the imperfections associated with their use, or how best to implement them when we determine that we need to do so. In this part we'll take a look at a variety of issues to do with operators. We begin, in Chapter 24, operator bool(), with the way in which operators can be used to indicate a notion of Boolean state, how this can be problematic due to unwanted conversions, and how it can be implemented in a maximally safe and portable way.

Next, in Chapter 25, Fast, Nonintrusive String Concatenation, we take an entirely upbeat look at string concatenation. In particular how we can achieve dramatic performance improvements by using a fast concatenation component that can be integrated with any string libraries in a nonintrusive fashion.

Then it's downbeat again in Chapter 26, What's Your Address? to look at the use—well, abuse, in the main—of the address of operator. We cover the several pitfalls with overloading this particular operator, but in the spirit of imperfect practitioners, we also look at some neat tricks to use if you choose to go ahead and overload.

The next chapter, Chapter 27, Subscript Operators, covers the debate between providing implicit conversion or subscript operators for array types, and provides some further insights into the imperfect relationship between arrays and pointers.

Chapter 28, Increment Operators, takes a brief but important look at the mechanism of overloading the increment and decrement operators, mounts the soapbox to berate the standard of education in our universities and ubiquitous inconsistency in professional practitioners, and then shows an efficient template-based return value monitor, which can be used to prevent abuse of your carefully crafted increment/decrement operators.

In the penultimate chapter, Chapter 29, Arithmetic Types, we catch up with our old friends the 64-bit integers from Chapter 1, and try and give them natural syntax. We ultimately fail, as we are wont to do, but we have some fun and learn some useful things in the process.

Finally, in the shortest chapter in the book, Chapter 30, Short-circuit!, we look at how overloading the logical operators tramples over C's short-circuit semantics, and recommend giving the practice a wide berth.



      Previous section   Next section