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