Boost logo

Boost Users :

From: Chris Ross (cross+boost_at_[hidden])
Date: 2008-06-20 15:16:02

  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

  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<Target, Source,
Traits>::operator<<(const Source&) [with Target = int, Source =
boost::any, Traits = std::char_traits<char>]’:
/usr/local/boost/include/boost/lexical_cast.hpp:1146: instantiated
from ‘Target boost::detail::lexical_cast(typename
boost::call_traits<Source>::param_type, CharT*, size_t) [with Target =
int, Source = boost::any, bool Unlimited = true, CharT =
/usr/local/boost/include/boost/lexical_cast.hpp:1170: instantiated
from ‘Target boost::lexical_cast(const Source&) [with Target = int,
Source = boost::any]’
../ instantiated from here
/usr/local/boost/include/boost/lexical_cast.hpp:588: error: no match for
‘operator<<’ in ‘((boost::detail::lexical_stream<int, boost::any,
std::char_traits<char> >*)this)->boost::detail::lexical_stream<int,
boost::any, std::char_traits<char> >::stream << input’
++/4.3.0/ostream:112: note: candidates are: std::basic_ostream<_CharT,
_Traits>& std::basic_ostream<_CharT,
_Traits>::operator<<(std::basic_ostream<_CharT, _Traits>&
(*)(std::basic_ostream<_CharT, _Traits>&)) [with _CharT = char, _Traits
= std::char_traits<char>]

(and more "candidates are" output lines)

  Line 51 of is:

        int v = boost::lexical_cast<int>(is);

  I know in this case that the subclass is returning a
boost::any<int64_t>. And, if I use boost::any_cast<int_64t>(is), I get
the right value out. But, if I use boost::any_cast<int>(is) it throws a
bad_any_cast, so I assume I need lexical_cast, or something like that,
in here since I don't know the source type.

  Can anyone else figure out why the above compile fails?

                         - Chris

Boost-users list run by williamkempf at, kalb at, bjorn.karlsson at, gregod at, wekempf at