boost::any is wrapper for arbitrary type, which provides value-semantics and lifetime management, just like my Wrapper class. And it would be preferable (because it is better tested as part of boost). But I think that it provides no visitation interface and no polymorphism. If that is so, how can it be used to solve the problem? I think that the OP needs something that can redirect the serialization of objects to Json, preferably in generic manner, because otherwise there will be some bulk of casting code that has to be maintained.
boost::variant is ok, but I find that it tries too hard to keep its storage embedded, like union type. The result is that it has weaker response in the presence of constructor exceptions, does cause more constructor invocations (both of which do not matter in this particular case) and still doesn't avoid dynamic allocation completely. As I understand, the initial versions used placement construction on top of another object, later versions used double storage in order to avoid the placement construction, the current version uses backup allocation on the heap. Anyway, it provides visitation mechanism, that can be used with generic visitor. And the code becomes shorter and less error-prone:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38
|
#include <iostream>
#include <list>
#include <algorithm>
#include <string>
#include <typeinfo>
#include <boost/variant.hpp>
using namespace std;
using namespace boost;
struct Serializer : public static_visitor<>
{
template <typename T>
void operator()(const T& data) const {
cout << data << " : " << typeid(T).name() << endl;
}
};
typedef variant<int, double, string> var_t;
int main()
{
list<var_t> l;
l.push_back(5);
l.push_back(5.0);
l.push_back(string("five"));
struct Processor {
static void process(var_t& data)
{
apply_visitor(Serializer(), data);
}
};
for_each(l.begin(), l.end(), Processor::process);
}
|
(After encountering boost::variant recently, I find it to be somewhat of an acquired taste. My [edit]
Wrapper in the previous post imposes no restriction on the types that it handles, just as long as they can be serialized. But unfortunately it is not tested so well so I can not recommend it wholeheartedly.)
Regards