Boost C++ Libraries Home Libraries People FAQ More

PrevUpHomeNext

Introduction

Boost.Convert builds on the boost::lexical_cast experience and takes those type conversion/transformation-related ideas further

Boost.Convert provides new and familiar conversion/transformation-related functionality such as:

Boost.Convert consists of two components:

The boost::convert() interface

The collection of pluggable converters is independent of the boost::convert() API facade and is designed to be extendible and extended over time. Currently the following converters are provided:

The converters provide new and familiar functionality and demonstrate how existing and independent conversion facilities might be incorporated in to the Boost.Convert framework. For example, the std::stream-based converter draws on the standard std::streams functionality and provides:

The following code demonstrates conversion of an array of integers from their textual hexadecimal representation. It assigns -1 to those which fail to convert:

boost::array<char const*, 3> strs = {{ " 5", "0XF", "not an int" }};
std::vector<int>             ints;
boost::cnv::cstream           cnv;

// Configure converter to read hexadecimal, skip (leading) white spaces.
cnv(std::hex)(std::skipws);

std::transform(strs.begin(), strs.end(), std::back_inserter(ints),
    boost::cnv::apply<int>(boost::cref(cnv)).value_or(-1));

BOOST_TEST(ints.size() == 3); // Number of values processed.
BOOST_TEST(ints[0] ==  5);    // " 5"
BOOST_TEST(ints[1] == 15);    // "0XF"
BOOST_TEST(ints[2] == -1);    // "not an int"


PrevUpHomeNext