Copyright ? 2002, 2003 Eric Friedman, Itay Maman Permission to copy, use, sell and distribute this software is granted provided this copyright notice appears in all copies. Permission to modify the code and to distribute modified code is granted provided this copyright notice appears in all copies, and a notice that the code was modified is included with the copyright notice. This software is provided "as is" without express or implied warranty, and with no claim as to its suitability for any purpose. Table of Contents
The Notable features of
Many times, during the development of a C++ program, the
programmer finds himself in need of manipulating several distinct
types in a uniform manner. Indeed, C++ features direct language
support for such types through its union { int i; double d; } u; u.d = 3.14; u.i = 3; // overwrites u.d (OK: u.d is a POD type) C++'s union { int i; std::string s; // illegal: std::string is not a POD type! } u; Clearly another approach is required. Typical solutions
feature the dynamic-allocation of objects, which are subsequently
manipulated through a common base type (often a virtual base class
[Hen01]
or, more dangerously, a However, solutions of this sort are highly error-prone, due to the following:
Furthermore, even when properly implemented, these solutions tend to incur a relatively significant abstraction penalty due to the use of the heap, virtual function calls, and polymorphic downcasts. The #include "boost/variant.hpp" #include <iostream> class my_visitor : public |