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
6 #include "./test.hpp"
7
8 #if defined(BOOST_CONVERT_IS_NOT_SUPPORTED)
main(int,char const * [])9 int main(int, char const* []) { return 0; }
10 #else
11
12 #include <boost/convert.hpp>
13 #include <boost/convert/detail/is_string.hpp>
14 #include <boost/convert/detail/is_callable.hpp>
15 #include <boost/detail/lightweight_test.hpp>
16
17 //[is_callable_declaration
18 namespace { namespace local
19 {
20 BOOST_DECLARE_IS_CALLABLE(can_call_funop, operator());
21 BOOST_DECLARE_IS_CALLABLE(can_call_func, func);
22 }}
23 //]
24
25 //[is_callable_classes_tested
26 namespace { namespace callable
27 {
operator ()__anon29d36b770211::callable::test128 struct test1 { int operator()(double, std::string) { return 0; }};
operator ()__anon29d36b770211::callable::test229 struct test2 { void operator()(double, std::string) {}};
operator ()__anon29d36b770211::callable::test330 struct test3 { void operator()(int) {}};
operator ()__anon29d36b770211::callable::test431 struct test4 { std::string operator()(int) const { return std::string(); }};
operator ()__anon29d36b770211::callable::test532 struct test5 { std::string operator()(int, std::string const& =std::string()) const { return std::string(); }};
operator ()__anon29d36b770211::callable::test633 struct test6 { template<typename T> std::string operator()(T) const { return std::string(); }};
operator ()__anon29d36b770211::callable::test734 struct test7 { template<typename T> T operator()(T) const { return T(); }};
35
func__anon29d36b770211::callable::test1136 struct test11 { int func(double, std::string) { return 0; }};
func__anon29d36b770211::callable::test1237 struct test12 { void func(double, std::string) {}};
func__anon29d36b770211::callable::test1338 struct test13 { void func(int) {}};
func__anon29d36b770211::callable::test1439 struct test14 { std::string func(int) const { return std::string(); }};
func__anon29d36b770211::callable::test1540 struct test15 { std::string func(int, std::string const& =std::string()) const { return std::string(); }};
func__anon29d36b770211::callable::test1641 struct test16 { template<typename T> std::string func(T) const { return std::string(); }};
func__anon29d36b770211::callable::test1742 struct test17 { template<typename T> T func(T) const { return T(); }};
43 }}
44 //]
45
46 static
47 void
test_is_callable()48 test_is_callable()
49 {
50 // C1. Unfortunately, passing 'double' where 'int' is expected returns 'true'.
51 // The same as the following (which successfully compiles):
52 // void fun(int) {}
53 // fun(double(1));
54
55 //[is_callable_usage1
56 BOOST_TEST((local::can_call_funop<callable::test1, int (double, std::string)>::value == true));
57 BOOST_TEST((local::can_call_funop<callable::test1, double (int, std::string)>::value == true));
58 BOOST_TEST((local::can_call_funop<callable::test1, void (double, std::string)>::value == true));
59 BOOST_TEST((local::can_call_funop<callable::test1, void (int, std::string)>::value == true));
60 BOOST_TEST((local::can_call_funop<callable::test1, void (int, char const*)>::value == true));
61 BOOST_TEST((local::can_call_funop<callable::test1, int (double, int)>::value == false));
62 BOOST_TEST((local::can_call_funop<callable::test1, int (double)>::value == false));
63
64 BOOST_TEST((local::can_call_funop<callable::test2, int (double, std::string)>::value == false));
65 BOOST_TEST((local::can_call_funop<callable::test2, void (double, std::string)>::value == true));
66 BOOST_TEST((local::can_call_funop<callable::test2, void ( int, std::string)>::value == true));
67 BOOST_TEST((local::can_call_funop<callable::test2, void ( int, char const*)>::value == true));
68 //]
69 BOOST_TEST((local::can_call_funop<callable::test3, void (double)>::value == true)); //C1
70 BOOST_TEST((local::can_call_funop<callable::test3, void (int)>::value == true));
71 BOOST_TEST((local::can_call_funop<callable::test3 const, void (int)>::value == false));
72
73 BOOST_TEST((local::can_call_funop<callable::test4 const, std::string (int)>::value == true));
74 BOOST_TEST((local::can_call_funop<callable::test4, std::string (int)>::value == true));
75 BOOST_TEST((local::can_call_funop<callable::test4 const, void (int)>::value == true));
76 BOOST_TEST((local::can_call_funop<callable::test4, void (int)>::value == true));
77
78 BOOST_TEST((local::can_call_funop<callable::test5, std::string (int, std::string)>::value == true));
79 BOOST_TEST((local::can_call_funop<callable::test5, std::string (int, std::string const&)>::value == true));
80 BOOST_TEST((local::can_call_funop<callable::test5, void (int, char const*)>::value == true));
81 BOOST_TEST((local::can_call_funop<callable::test5, std::string (int)>::value == true));
82 BOOST_TEST((local::can_call_funop<callable::test5 const, std::string (int)>::value == true));
83 BOOST_TEST((local::can_call_funop<callable::test5, void (int)>::value == true));
84 BOOST_TEST((local::can_call_funop<callable::test5 const, void (int)>::value == true));
85 BOOST_TEST((local::can_call_funop<callable::test5, void (char const*)>::value == false));
86 BOOST_TEST((local::can_call_funop<callable::test5 const, void (char const*)>::value == false));
87
88 BOOST_TEST((local::can_call_funop<callable::test6, std::string (int)>::value == true));
89 BOOST_TEST((local::can_call_funop<callable::test6, std::string (std::string)>::value == true));
90 BOOST_TEST((local::can_call_funop<callable::test6, void (int)>::value == true));
91 BOOST_TEST((local::can_call_funop<callable::test6, void (std::string)>::value == true));
92
93 BOOST_TEST((local::can_call_funop<callable::test7, std::string (int)>::value == false));
94 BOOST_TEST((local::can_call_funop<callable::test7, std::string (std::string)>::value == true));
95 BOOST_TEST((local::can_call_funop<callable::test7, void (int)>::value == true));
96 BOOST_TEST((local::can_call_funop<callable::test7, void (std::string)>::value == true));
97
98 //[is_callable_usage2
99 BOOST_TEST((local::can_call_func<callable::test11, int (double, std::string)>::value == true));
100 BOOST_TEST((local::can_call_func<callable::test11, double (int, std::string)>::value == true));
101 BOOST_TEST((local::can_call_func<callable::test11, void (double, std::string)>::value == true));
102 BOOST_TEST((local::can_call_func<callable::test11, void (int, std::string)>::value == true));
103 BOOST_TEST((local::can_call_func<callable::test11, void (int, char const*)>::value == true));
104 BOOST_TEST((local::can_call_func<callable::test11, int (double, int)>::value == false));
105 BOOST_TEST((local::can_call_func<callable::test11, int (double)>::value == false));
106
107 BOOST_TEST((local::can_call_func<callable::test12, int (double, std::string)>::value == false));
108 BOOST_TEST((local::can_call_func<callable::test12, void (double, std::string)>::value == true));
109 BOOST_TEST((local::can_call_func<callable::test12, void ( int, std::string)>::value == true));
110 BOOST_TEST((local::can_call_func<callable::test12, void ( int, char const*)>::value == true));
111 //]
112 BOOST_TEST((local::can_call_func<callable::test13, void (double)>::value == true)); //C1
113 BOOST_TEST((local::can_call_func<callable::test13, void (int)>::value == true));
114 BOOST_TEST((local::can_call_func<callable::test13 const, void (int)>::value == false));
115
116 BOOST_TEST((local::can_call_func<callable::test14 const, std::string (int)>::value == true));
117 BOOST_TEST((local::can_call_func<callable::test14, std::string (int)>::value == true));
118 BOOST_TEST((local::can_call_func<callable::test14 const, void (int)>::value == true));
119 BOOST_TEST((local::can_call_func<callable::test14, void (int)>::value == true));
120
121 BOOST_TEST((local::can_call_func<callable::test15, std::string (int, std::string)>::value == true));
122 BOOST_TEST((local::can_call_func<callable::test15, std::string (int, std::string const&)>::value == true));
123 BOOST_TEST((local::can_call_func<callable::test15, void (int, char const*)>::value == true));
124 BOOST_TEST((local::can_call_func<callable::test15, std::string (int)>::value == true));
125 BOOST_TEST((local::can_call_func<callable::test15 const, std::string (int)>::value == true));
126 BOOST_TEST((local::can_call_func<callable::test15, void (int)>::value == true));
127 BOOST_TEST((local::can_call_func<callable::test15 const, void (int)>::value == true));
128 BOOST_TEST((local::can_call_func<callable::test15, void (char const*)>::value == false));
129 BOOST_TEST((local::can_call_func<callable::test15 const, void (char const*)>::value == false));
130
131 BOOST_TEST((local::can_call_func<callable::test16, std::string (int)>::value == true));
132 BOOST_TEST((local::can_call_func<callable::test16, std::string (std::string)>::value == true));
133 BOOST_TEST((local::can_call_func<callable::test16, void (int)>::value == true));
134 BOOST_TEST((local::can_call_func<callable::test16, void (std::string)>::value == true));
135
136 BOOST_TEST((local::can_call_func<callable::test17, std::string (int)>::value == false));
137 BOOST_TEST((local::can_call_func<callable::test17, std::string (std::string)>::value == true));
138 BOOST_TEST((local::can_call_func<callable::test17, void (int)>::value == true));
139 BOOST_TEST((local::can_call_func<callable::test17, void (std::string)>::value == true));
140 }
141
142 int
main(int,char const * [])143 main(int, char const* [])
144 {
145 //[is_callable_usage
146 //]
147
148 test_is_callable();
149
150 BOOST_TEST(boost::cnv::is_string<direction>::value == false);
151 BOOST_TEST(boost::cnv::is_string<std::string>::value == true);
152 BOOST_TEST(boost::cnv::is_string<std::wstring>::value == true);
153 BOOST_TEST(boost::cnv::is_string<my_string>::value == true);
154 BOOST_TEST(boost::cnv::is_string<int>::value == false);
155
156 return boost::report_errors();
157 }
158
159 #endif
160