]>
git.proxmox.com Git - ceph.git/blob - ceph/src/boost/libs/convert/test/callable.cpp
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.
8 #if defined(BOOST_CONVERT_INTEL_SFINAE_BROKEN) && 1 < 0
9 int main(int, char const* []) { return 0; }
12 #include <boost/convert.hpp>
13 #include <boost/convert/lexical_cast.hpp>
14 #include <boost/detail/lightweight_test.hpp>
15 #include <boost/function.hpp>
16 #include <boost/bind.hpp>
20 using boost::lexical_cast
;
23 void plain_old_func(string
const& value_in
, boost::optional
<int>& value_out
)
28 value_out
= lexical_cast
<int>(value_in
);
35 template<typename TypeIn
, typename TypeOut
>
37 convert_all(TypeIn
const&, boost::optional
<TypeOut
>&)
41 template<typename Type
>
43 assign(boost::optional
<Type
>& value_out
, Type
const& value_in
)
50 template<typename TypeIn
, typename TypeOut
>
52 operator()(TypeIn
const&, boost::optional
<TypeOut
>&) const
58 struct take_double
{ void operator()(double, boost::optional
<string
>&) const {}};
59 struct take_int
{ void operator()(int, boost::optional
<string
>&) const {}};
65 // Declared for all types.
66 template<typename TypeIn
> void operator()(TypeIn
, boost::optional
<string
>&) const;
69 // Defined only for certain types.
70 template<> void double_only::operator()<double>(double, boost::optional
<string
>&) const {}
74 main(int, char const* [])
76 typedef boost::function
<void (string
const& value_in
, boost::optional
<int>&)> boost_func
;
78 char const* const str
= "-12";
80 // Testing old-function-based converter.
82 int v01
= convert
<int>(str
, plain_old_func
).value_or(-1);
84 // Testing boost::function-based converter.
85 int v02
= convert
<int>(str
, boost_func(plain_old_func
)).value_or(-1);
86 // Testing crazy boost::bind-based converter.
88 int v03
= convert
<int>(str
,
89 boost::bind(assign
<int>, _2
,
90 boost::bind(lexical_cast
<int, string
>, _1
))).value_or(-1);
92 BOOST_TEST(v01
== -12);
93 BOOST_TEST(v02
== -12);
94 BOOST_TEST(v03
== -12);
96 convert
<int>(str
, convert_all
<string
, int>);
97 convert
<string
>(11, convert_all
<int, string
>);
98 convert
< int>(str
, converter1());
99 convert
<string
>(11, converter1());
100 convert
<string
>(11.23, take_double());
101 convert
<string
>(11, take_int());
103 convert
<string
>(11, take_double()); // Compiler applies int-to-double promotion to call the converter.
104 convert
<string
>(11.23, take_int()); // Compiler applies double-to-int implicit truncation.
107 convert
<string
>(11.23, double_only()); // Fine.
108 // convert<string>(11, double_only()); // Fails: undefined reference to double_only::operator()<int>
111 return boost::report_errors();