summaryrefslogtreecommitdiffstats
path: root/src/boost/libs/convert/example/lexical_cast.cpp
diff options
context:
space:
mode:
authorDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
committerDaniel Baumann <daniel.baumann@progress-linux.org>2024-04-27 18:24:20 +0000
commit483eb2f56657e8e7f419ab1a4fab8dce9ade8609 (patch)
treee5d88d25d870d5dedacb6bbdbe2a966086a0a5cf /src/boost/libs/convert/example/lexical_cast.cpp
parentInitial commit. (diff)
downloadceph-upstream.tar.xz
ceph-upstream.zip
Adding upstream version 14.2.21.upstream/14.2.21upstream
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'src/boost/libs/convert/example/lexical_cast.cpp')
-rw-r--r--src/boost/libs/convert/example/lexical_cast.cpp35
1 files changed, 35 insertions, 0 deletions
diff --git a/src/boost/libs/convert/example/lexical_cast.cpp b/src/boost/libs/convert/example/lexical_cast.cpp
new file mode 100644
index 00000000..5527cf14
--- /dev/null
+++ b/src/boost/libs/convert/example/lexical_cast.cpp
@@ -0,0 +1,35 @@
+// Copyright (c) 2009-2016 Vladimir Batov.
+// Use, modification and distribution are subject to the Boost Software License,
+// Version 1.0. See http://www.boost.org/LICENSE_1_0.txt.
+
+//[lexical_cast_headers1
+#include <boost/convert.hpp>
+#include <boost/convert/lexical_cast.hpp>
+#include <boost/detail/lightweight_test.hpp>
+
+using std::string;
+using boost::convert;
+using boost::lexical_cast;
+
+struct boost::cnv::by_default : boost::cnv::lexical_cast {};
+//]
+
+int
+main(int, char const* [])
+{
+ //[lexical_cast_example1
+ int i1 = lexical_cast<int>("123"); // Throws if the conversion fails.
+ int i2 = convert<int>("123").value(); // Throws if the conversion fails.
+ int i3 = convert<int>("uhm").value_or(-1); // Returns -1 if the conversion fails.
+ string s1 = lexical_cast<string>(123);
+ string s2 = convert<string>(123).value();
+
+ BOOST_TEST(i1 == 123);
+ BOOST_TEST(i2 == 123);
+ BOOST_TEST(i3 == -1);
+ BOOST_TEST(s1 == "123");
+ BOOST_TEST(s2 == "123");
+ //]
+
+ return boost::report_errors();
+}