
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<output_streamable> my_streamable_any; It's not an official Boost library, but thus far it is quite impressive.
The Electric Monk "Quidquid latine dictum sit, altum viditur!"
:) Doug