• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 
5 //[lexical_cast_headers1
6 #include <boost/convert.hpp>
7 #include <boost/convert/lexical_cast.hpp>
8 #include <boost/detail/lightweight_test.hpp>
9 
10 using std::string;
11 using boost::convert;
12 using boost::lexical_cast;
13 
14 struct boost::cnv::by_default : boost::cnv::lexical_cast {};
15 //]
16 
17 int
main(int,char const * [])18 main(int, char const* [])
19 {
20     //[lexical_cast_example1
21     int    i1 = lexical_cast<int>("123");         // Throws if the conversion fails.
22     int    i2 = convert<int>("123").value();      // Throws if the conversion fails.
23     int    i3 = convert<int>("uhm").value_or(-1); // Returns -1 if the conversion fails.
24     string s1 = lexical_cast<string>(123);
25     string s2 = convert<string>(123).value();
26 
27     BOOST_TEST(i1 == 123);
28     BOOST_TEST(i2 == 123);
29     BOOST_TEST(i3 == -1);
30     BOOST_TEST(s1 == "123");
31     BOOST_TEST(s2 == "123");
32     //]
33 
34     return boost::report_errors();
35 }
36