ive years ago, I explained how to simulate heterogeneous containers
by storing raw pointers to polymorphic objects in an STL container. C++ has come along way since: tuples now allow you store arbitrary types in one object. Alternatively, you can replace the raw pointers with shared_ptr
. And yet, a heterogeneous container that is free from the hassles of pointer syntax and other arbitrary restrictions is still elusive. This month's solution presents another technique for implementing heterogeneous containers using the Boost.Any
How to store objects of different types in an STL container without the restrictions and complexities of pointers?
Use the Boost.Any library to safely store objects of arbitrary datatypes in a container.