Boost Conversion Library polymorphic_cast polymorphic_downcast lexical_cast

#include <boost/cast.hpp> 

polymorphic_cast polymorphic_downcast

<boost/lexical_cast.hpp>

lexical_cast bad_lexical_cast

int main(int argc, char * argv[])
{
    using boost::lexical_cast;
    using boost::bad_lexical_cast;

    std::vector<short> args;

    while(*++argv)
    {
        try
        {
            args.push_back(lexical_cast<short>(*argv));
        }
        catch(bad_lexical_cast &)
        {
            args.push_back(0);
        }
    }
    ...
}

For more involved conversions, such as where precision or formatting need tighter control than is offered by the default behavior of lexical_cast, the conventional std::stringstream approach is recommended. Where the conversions are numeric to numeric, boost::numeric_cast may offer more reasonable behavior than lexical_cast.
原文地址:https://www.cnblogs.com/logitechlike/p/2835448.html