boost::any and boost::lexical_cast

I'm having some problems getting boost::lexical_cast to work for me.
I have an abstract class, and it calls a pure virtual function which
returns a boost::any. I don't know the type of the boost::any, but in
the place I'm working now, I pretty much can assume it's boolean, but it
could be a 'bool' data type, or an 'int', or a 'short', or any other
thing depending on how the boost::any that the method returned to me was
constructed.
When I try to use boost::lexical_cast<int>(anyvar), I get compile
errors as:
/usr/local/boost/include/boost/lexical_cast.hpp: In member function
‘bool boost::detail::lexical_stream

AMDG Chris Ross wrote:
When I try to use boost::lexical_cast<int>(anyvar), I get compile errors as:
Line 51 of db.cc is:
int v = boost::lexical_cast<int>(is);
Can anyone else figure out why the above compile fails?
any does not support streaming, which is required for lexical_cast. std::cout << is << std::endl; should give similar errors. In Christ, Steven Watanabe

On Jun 21, 2008, at 12:22, Steven Watanabe wrote:
Chris Ross wrote:
When I try to use boost::lexical_cast<int>(anyvar), I get compile errors [...] Can anyone else figure out why the above compile fails?
any does not support streaming, which is required for lexical_cast.
std::cout << is << std::endl;
should give similar errors.
Okay, that makes sense. Thank you for explaining why lexical_cast isn't able to do what I hoped it would with my boost::any. Does anyone have a suggestion as to how I can get a value out of a boost::any for which I don't know the specific type stored in it? A subclass method is returning the boost::any, which is conceptually a bool, but different subclasses may implement it as a bool, int, int64_t, etc. I can just static-cast them to bool if I can get the value out of the boost::any, but boost::any_cast gives a bad_any_cast unless I cast it to exactly the right type (which as I noted, the super-class doesn't know) Thanks. Any suggestions would be appreciated. - Chris

On Sun, Jun 22, 2008 at 07:22, Chris Ross
Does anyone have a suggestion as to how I can get a value out of a boost::any for which I don't know the specific type stored in it? A subclass method is returning the boost::any, which is conceptually a bool, but different subclasses may implement it as a bool, int, int64_t, etc. I can just static-cast them to bool if I can get the value out of the boost::any, but boost::any_cast gives a bad_any_cast unless I cast it to exactly the right type (which as I noted, the super-class doesn't know)
This is the classic example of "why you didn't really want to use any
in the first place". If you're going to do anything with it (other
than pass it around), then you know something about its type, so any
more general than you really need. It's just like in unityped (aka
Dynamically Typed) languages.
Perhaps you can pass a boost::function

Does anyone have a suggestion as to how I can get a value out of a boost::any for which I don't know the specific type stored in it? if you know the set of types you operate on consider using boost::variant which supports visitor pattern for dispatching of currently stored type.
A subclass method is returning the boost::any, which is conceptually a bool, but different subclasses may implement it as a bool, int, int64_t, etc. I can just static-cast them to bool if I can get the value out of the boost::any, but boost::any_cast gives a bad_any_cast unless I cast it to exactly the right type (which as I noted, the super-class doesn't know)
Another approach could be to introduce an additional level of indirection. Another Holder type of your types and implement for this type operator<<. Than you can always retrieve from boost::any your holder_type instance and apply to your holder_type instance lexical_cast. Hope that helps. It is difficult to make suggestions, because we don't if you are allowed to modify the interface. Best Regards, Ovanes

Ovanes Markarian wrote:
Does anyone have a suggestion as to how I can get a value out of a boost::any for which I don't know the specific type stored in it? if you know the set of types you operate on consider using boost::variant which supports visitor pattern for dispatching of currently stored type.
A subclass method is returning the boost::any, which is conceptually a bool, but different subclasses may implement it as a bool, int, int64_t, etc. I can just static-cast them to bool if I can get the value out of the boost::any, but boost::any_cast gives a bad_any_cast unless I cast it to exactly the right type (which as I noted, the super-class doesn't know)
Another approach could be to introduce an additional level of indirection. Another Holder type of your types and implement for this type operator<<. Than you can always retrieve from boost::any your holder_type instance and apply to your holder_type instance lexical_cast.
Hope that helps. It is difficult to make suggestions, because we don't if you are allowed to modify the interface.
Best Regards, Ovanes If you have "the Boost book" (Beyond the C++ Standard Library: An Introduction to Boost), I think chapter 6 implements something like this to allow streaming of boost::any types.

On Jun 22, 2008, at 11:49, Ovanes Markarian wrote:
Does anyone have a suggestion as to how I can get a value out of a boost::any for which I don't know the specific type stored in it? if you know the set of types you operate on consider using boost::variant which supports visitor pattern for dispatching of currently stored type.
Yes, I'll have to look into boost::variant. Thanks for that pointer, all.
Hope that helps. It is difficult to make suggestions, because we don't if you are allowed to modify the interface.
Right. And a very good point to bring up. I am constructing this
100% myself from scratch, so anything *can* be done.
The super-class in which I'm trying to extract this value is a
generic database access object. The boost::any is being returned from
a "get me the value of column X in table Y" method, implemented by any
subclass that implements the database-specific layer. So, the values
could literally be any value that could be stored in any data value in
any database, which is why I was trying to use something very generic.
This particular function, the first I'm implementing in the design
as I'm building it out, is asking for a value it knows will be
effectively boolean. But, depending on the underlying database, may
be an int, or some other type. The first case I'm coding is using an
sqlite3 subclass of the database object, and there is no boolean
type. There is an integer type, and since that database (in current
versions) supports 64-bit integers, I've coded that method to return a
boost::any

On Jun 22, 2008, at 18:50, Igor R wrote:
if you know the set of types you operate on consider using boost::variant which supports visitor pattern for dispatching of currently stored type.
Besides, if I'm not mistaken, there already exists operator << for boost::variant (which internally performs the visiting)...
Yeah. Using boost::variant looks a little unwieldy, as I'd have to deal with results from the database as complicated boost::variants with at least 5 possibilities, but it would be doable with something as simple as a typedef, I'd guess. Sub-ideal, but may be a reasonable way to do it. - Chris
participants (6)
-
Chris Ross
-
Igor R
-
Kenny Riddile
-
Ovanes Markarian
-
Scott McMurray
-
Steven Watanabe