1 
2 // Copyright (C) 2009-2012 Lorenzo Caminiti
3 // Distributed under the Boost Software License, Version 1.0
4 // (see accompanying file LICENSE_1_0.txt or a copy at
5 // http://www.boost.org/LICENSE_1_0.txt)
6 // Home at http://www.boost.org/libs/local_function
7 
8 #include <boost/local_function.hpp>
9 #include <boost/functional/overloaded_function.hpp>
10 #include <boost/typeof/std/string.hpp>
11 #include <boost/detail/lightweight_test.hpp>
12 #include <string>
13 
add_i(int x,int y)14 int add_i(int x, int y) { return x + y; }
15 
main(void)16 int main(void) {
17     std::string s = "abc";
18     std::string BOOST_LOCAL_FUNCTION(
19             (const bind& s) (const std::string& x) ) {
20         return s + x;
21     } BOOST_LOCAL_FUNCTION_NAME(add_s)
22 
23     double d = 1.23;
24     double BOOST_LOCAL_FUNCTION( (const bind d) (double x)
25             (double y)(default 0) ) {
26         return d + x + y;
27     } BOOST_LOCAL_FUNCTION_NAME(add_d)
28 
29     boost::overloaded_function<
30           std::string (const std::string&)
31         , double (double)
32         , double (double, double)
33         , int (int, int)
34     > add(add_s, add_d, add_d, add_i);
35 
36     BOOST_TEST(add("xyz") == "abcxyz");
37     BOOST_TEST((4.44 - add(3.21)) <= 0.001); // Equal within precision.
38     BOOST_TEST((44.44 - add(3.21, 40.0)) <= 0.001); // Equal within precision.
39     BOOST_TEST(add(1, 2) == 3);
40     return boost::report_errors();
41 }
42 
43