lexical_cast.cpp 1.0 KB

1234567891011121314151617181920212223242526272829303132333435
  1. // Copyright (c) 2009-2016 Vladimir Batov.
  2. // Use, modification and distribution are subject to the Boost Software License,
  3. // Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
  4. //[lexical_cast_headers1
  5. #include <boost/convert.hpp>
  6. #include <boost/convert/lexical_cast.hpp>
  7. #include <boost/detail/lightweight_test.hpp>
  8. using std::string;
  9. using boost::convert;
  10. using boost::lexical_cast;
  11. struct boost::cnv::by_default : boost::cnv::lexical_cast {};
  12. //]
  13. int
  14. main(int, char const* [])
  15. {
  16. //[lexical_cast_example1
  17. int i1 = lexical_cast<int>("123"); // Throws if the conversion fails.
  18. int i2 = convert<int>("123").value(); // Throws if the conversion fails.
  19. int i3 = convert<int>("uhm").value_or(-1); // Returns -1 if the conversion fails.
  20. string s1 = lexical_cast<string>(123);
  21. string s2 = convert<string>(123).value();
  22. BOOST_TEST(i1 == 123);
  23. BOOST_TEST(i2 == 123);
  24. BOOST_TEST(i3 == -1);
  25. BOOST_TEST(s1 == "123");
  26. BOOST_TEST(s2 == "123");
  27. //]
  28. return boost::report_errors();
  29. }