onversion operators are a dilemma. While they certainly make the usage of certain objects convenient and intuitive, they also compromise type safety, ultimately leading to unexpected runtime behavior. The following sections present a technique that significantly reduces the potential hazards of implicit conversions to bool
. This technique is now becoming a de-facto standard in C++ libraries, like Boost.
You want to add a conversion operator to your class, but you're concerned about the perils of implicit conversions.
Use the "indirect conversion" idiom to thwart undesirable conversions while still permitting well-behaved conversions.