boost::any, string conversion and serialization

How to get a string representation of of boost::any value? for example, is the following approach correct? boost::any t(2); std::string str = boost::any_caststd::string(t); In addition, from searching archieves I understand that there is no serialization for boost::any. Currently I plan to examine "name" in type_info returned from boost::any::type() and serialize accordingly. I'll appreciate any advise for better approach.

for example, is the following approach correct? boost::any t(2); std::string str = boost::any_caststd::string(t);
I don't believe that will work. I don't think boost::any supports implicit conversions like that.
From the docs: [Boost Any is in a class of] Discriminated types that contain values of different types but do not attempt conversion between them, i.e. 5 is held strictly as an int and is not implicitly convertible either to "5" or to 5.0. Their indifference to interpretation but awareness of type effectively makes them safe, generic containers of single values, with no scope for surprises from ambiguous conversions.
For that you would need something like: std::string str = boost::lexical_caststd::string(boost::any_cast<int>(t));
In addition, from searching archieves I understand that there is no serialization for boost::any. Currently I plan to examine "name" in type_info returned from boost::any::type() and serialize accordingly. I'll appreciate any advise for better approach.
I'm afraid I can't comment on this. . . Nate

Nathan Crookston
.
For that you would need something like:
std::string str = boost::lexical_caststd::string(boost::any_cast<int>(t));
That was my first choice except that in my situation I don't know the type of boost::any in advance. Issue of serialization of boost::any pops up every now and then and in almost the same situation - using boost::any for storing some kind of properties in the container.
participants (3)
-
Archie14
-
Maxime van Noppen
-
Nathan Crookston