default_converter.cpp 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839
  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. #include <boost/convert.hpp>
  6. #include <boost/convert/stream.hpp>
  7. #include <boost/detail/lightweight_test.hpp>
  8. #ifdef ONLY_FOR_DEMONSTRATION_PURPOSES
  9. //[default_converter_declaration_simple
  10. struct boost::cnv::by_default : boost::cnv::cstream {};
  11. //]
  12. #endif
  13. //[default_converter_declaration_formatted
  14. struct boost::cnv::by_default : boost::cnv::cstream
  15. {
  16. by_default() { (*this)(std::uppercase)(std::hex); }
  17. };
  18. //]
  19. int
  20. main(int, char const* [])
  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. // 'i' and 's' are converted using boost::cnv::cstream
  27. // with std::uppercase and std::hex formatting applied.
  28. BOOST_TEST(i == 15); // 15(10) = F(16)
  29. BOOST_TEST(s == "FF"); // 255(10) = FF(16)
  30. //]
  31. return boost::report_errors();
  32. }