// Boost.Convert test and usage example // Copyright (c) 2009-2014 Vladimir Batov. // Use, modification and distribution are subject to the Boost Software License, // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt. #include #ifdef BOOST_CONVERT_BROKEN_COMPILER int main(int, char const* []) { return 0; } #else #include #include #include #include #include #include "./test.hpp" using std::string; using boost::convert; using boost::lexical_cast; //[callable_example1 void plain_old_func(string const& value_in, boost::optional& value_out) //] { try { value_out = lexical_cast(value_in); } catch (...) { } } template void convert_all(TypeIn const&, boost::optional&) { } template void assign(boost::optional& value_out, Type const& value_in) { value_out = value_in; } struct converter1 { template void operator()(TypeIn const&, boost::optional&) const { } }; struct take_double { void operator()(double const&, boost::optional&) const {}}; struct take_int { void operator()(int const&, boost::optional&) const {}}; int main(int argc, char const* argv[]) { typedef boost::function&)> boost_func; char const* const str = "-12"; // Testing old-function-based converter. //[callable_example2 int v01 = convert(str, plain_old_func).value_or(-1); //] // Testing boost::function-based converter. int v02 = convert(str, boost_func(plain_old_func)).value_or(-1); // Testing crazy boost::bind-based converter. //[callable_example3 int v03 = convert(str, boost::bind(assign, _2, boost::bind(lexical_cast, _1))).value_or(-1); //] BOOST_TEST(v01 == -12); BOOST_TEST(v02 == -12); BOOST_TEST(v03 == -12); converter1 cnv1; take_double cnv2; take_int cnv3; convert(str, convert_all); convert(11, convert_all); convert< int>(str, cnv1); convert(11, cnv1); convert(11.23, cnv2); convert(11, cnv2); convert(11, cnv3); convert(11.23, cnv3); // ^^^^^^^^^^^^^^^^^^^^^^^^ // When I call convert(11, take_int()); // MSVC8-11 fail to compile lines 84-89. // So, I am trying to figure out what they do not like return boost::report_errors(); } #endif