1 // Boost.Convert test and usage example 2 // Copyright (c) 2009-2014 Vladimir Batov. 3 // Use, modification and distribution are subject to the Boost Software License, 4 // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt. 5 6 #include <boost/convert.hpp> 7 #include <boost/convert/stream.hpp> 8 #include <boost/detail/lightweight_test.hpp> 9 10 #ifdef ONLY_FOR_DEMONSTRATION_PURPOSES 11 //[default_converter_declaration_simple 12 struct boost::cnv::by_default : public boost::cnv::cstream {}; 13 //] 14 #endif 15 //[default_converter_declaration_formatted by_defaultboost::cnv::by_default16struct boost::cnv::by_default : public boost::cnv::cstream { by_default() { (*this)(std::uppercase)(std::hex); }}; 17 //] 18 19 int main(int argc,char const * argv[])20main(int argc, char const* argv[]) 21 { 22 //[default_converter_example1 23 // No explicit converter provided. boost::cnv::by_default is used. 24 int i = boost::convert<int>("F").value_or(-1); 25 std::string s = boost::convert<std::string>(255).value_or("bad"); 26 27 // 'i' and 's' are converted using boost::cnv::cstream 28 // with std::uppercase and std::hex formatting applied. 29 30 BOOST_TEST(i == 15); // 15(10) = F(16) 31 BOOST_TEST(s == "FF"); // 255(10) = FF(16) 32 //] 33 34 return boost::report_errors(); 35 } 36 37