1 // Boost.Convert test and usage example 2 // Copyright (c) 2009-2016 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 : boost::cnv::cstream {}; 13 //] 14 #endif 15 //[default_converter_declaration_formatted 16 struct boost::cnv::by_default : boost::cnv::cstream 17 { by_defaultboost::cnv::by_default18 by_default() { (*this)(std::uppercase)(std::hex); } 19 }; 20 //] 21 22 int main(int,char const * [])23main(int, char const* []) 24 { 25 //[default_converter_example1 26 // No explicit converter provided. boost::cnv::by_default is used. 27 int i = boost::convert<int>("F").value_or(-1); 28 std::string s = boost::convert<std::string>(255).value_or("bad"); 29 30 // 'i' and 's' are converted using boost::cnv::cstream 31 // with std::uppercase and std::hex formatting applied. 32 33 BOOST_TEST(i == 15); // 15(10) = F(16) 34 BOOST_TEST(s == "FF"); // 255(10) = FF(16) 35 //] 36 37 return boost::report_errors(); 38 } 39 40