Home | Libraries | People | FAQ | More |
The explicit converter as in
int i = boost::convert<int>("123", converter).value();
provides considerable flexibility, configurability and efficiency. However,
in certain contexts that might be not that important or even counter-productive
if, for example, an application relies on certain consistent behavior associated
with one particular converter type and configuration. To accommodate such a
scenario Boost.Convert introduces the concept of the
default converter implemented as boost::cnv::by_default
.
Important | |
---|---|
There is no default converter set by default. |
Consequently, without additional configuration steps the following call will fail to compile:
int i = boost::convert<int>("123").value(); // No converter provided
However, after boost::cnv::by_default
is defined simply as:
struct boost::cnv::by_default : boost::cnv::cstream {};
or potentially configured with additional formatting:
struct boost::cnv::by_default : boost::cnv::cstream { by_default() { (*this)(std::uppercase)(std::hex); } };
the code compiles and deploys boost::cnv::cstream
when boost::convert()
is called without an explicitly supplied
converter:
// No explicit converter provided. boost::cnv::by_default is used. int i = boost::convert<int>("F").value_or(-1); std::string s = boost::convert<std::string>(255).value_or("bad"); // 'i' and 's' are converted using boost::cnv::cstream // with std::uppercase and std::hex formatting applied. BOOST_TEST(i == 15); // 15(10) = F(16) BOOST_TEST(s == "FF"); // 255(10) = FF(16)
The trade-off for the convenience is the rigid converter configuration (which
in certain contexts might be the desired behavior) and a potential performance
impact. When a converter is not provided explicitly, the default converter
is created, potentially configured, deployed and destroyed for every boost::convert()
call. Consequently, if efficiency of this
particular component is important, then the implementation of boost::cnv::by_default
will need to take that into account and to make sure those operations are cheap.