boost::lexical_cast extension for boost::any ...
I have a need to cast a boost::any with an arbitrary contained type to
another type, using a boost::lexical_cast for example.
Consider the following:
#include
On Fri, 4 Apr 2003, Electric Monk wrote:
Can anyone suggest a simple way around this? My own musings on the subject have lead me to thinking that there needs to be a simple way to extract the contained value, or write it to a stream, without knowing its type.
The simplest method I can see would be to add a function to boost::any::placeholder which either returns a string representation of the held value or writes the held value to a std::basic_ostream. This should allow a simple boost::lexical_cast conversion for boost::any to any arbitrary type.
Comments? Have I missed anything major?
Oh, and yes, I know that this goes beyond the intent of boost::any. I could easily create my own variant of boost::any which acts in the way I want, but I think this kind of functionality is useful in a number of cases, so it'd be nice to have the best solution possible... which is best discussed by the people here, who are much better versed in the boost library than I :)
Alexander Nasonov is working on dynamic_any, which is a _much_ more
powerful version of boost::any that allows you to include arbitrary
operations. For your situation, you would want something like:
typedef dynamic_any
The Electric Monk "Quidquid latine dictum sit, altum viditur!"
:) Doug
participants (2)
-
Douglas Paul Gregor
-
Electric Monk