Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::any, string conversion and serialization
From: Nathan Crookston (nathan.crookston_at_[hidden])
Date: 2010-05-19 12:15:05


> for example, is the following approach correct?
> boost::any t(2);
> std::string str = boost::any_cast<std::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_cast<std::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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net