Boost logo

Boost Users :

From: Klaus Nowikow (nowikow_at_[hidden])
Date: 2002-10-28 06:03:23


Hi!

I am trying to convert a string to a boost::dynamic_bitset
using boost::lexical_cast, as in this example:

template <class Value>
bool GetValue(const std::string& str, Value& val)
{
    try
    {
        val = boost::lexical_cast<Value>(str);
    }
    catch(boost::bad_lexical_cast&)
    {
        return false;
    }

    return true;
}

int main()
{
    std::string str = "01";
    boost::dynamic_bitset<> bits;

    if(!GetValue(str, bits))
    {
        [...]
    }
}

The lexical_cast always throws an exception.
The problem seems to be in lexical_cast.hpp,
where a temporary object is created:

Target result;
if(!(interpreter << arg) || !(interpreter >> result) ||
   !(interpreter >> std::ws).eof())
   throw bad_lexical_cast();

and in dynamic_bitset.hpp, where
operator>>(std::basic_istream<CharT, Traits>&, dynamic_bitset<Block, Allocator>&)
iterates over the bitset using the size() (which is, of course, 0).

I sure could solve my particular problem by providing an explicit
template instantiation of the GetValue() function, but I don't
want to do this because the code is for a library, and I want to reduce
references to other libraries to an absolute minimum.

Did I miss something here, or is there a workaround for this problem?

Regards,

Klaus


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