Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

boost::cnv::strtol Converter

Basic Deployment
Formatting Support
Numeric Base (bin, oct, dec, hex)
Field Width, Fill Character and Adjustment
Leading Whitespace Characters
Floating-Point Precision
Supported String Types
Wide String
Custom String Types

The converter started as a deployment example (with the std::strtol family of functions as its conversion engine) and a part of the performance-test set. Surprisingly, the converter showed fairly decent all-around performance (see Converters Compared) and, consequently, has been rewritten and extended to provide additional conversion support and formatting. At the moment it seems to be a good choice with moderate formatting facilities and adequate performance.

It should be noted though that the converter is nowhere as mature as boost::cnv::lexical_cast or boost::cnv::stream and, therefore, bugs are to be expected.

#include <boost/convert.hpp>
#include <boost/convert/strtol.hpp>

using std::string;
using std::wstring;
using boost::convert;

struct boost::cnv::by_default : boost::cnv::strtol {};

string const     bad_str = "not an int";
string const     std_str = "-11";
char const* const  c_str = "-12";
boost::string_view v_str = boost::string_view(c_str, 2);

BOOST_TEST( -1 == convert<int>(bad_str).value_or(-1));
BOOST_TEST(-11 == convert<int>(std_str).value());
BOOST_TEST(-12 == convert<int>(  c_str).value());
BOOST_TEST( -1 == convert<int>(  v_str).value_or(0));


PrevUpHomeNext