| [12] | 1 | // Copyright David Abrahams, Daniel Wallin 2003. Use, modification and |
|---|
| 2 | // distribution is subject to the Boost Software License, Version 1.0. |
|---|
| 3 | // (See accompanying file LICENSE_1_0.txt or copy at |
|---|
| 4 | // http://www.boost.org/LICENSE_1_0.txt |
|---|
| 5 | |
|---|
| 6 | #include <boost/parameter.hpp> |
|---|
| 7 | #include <cassert> |
|---|
| 8 | #include <string> |
|---|
| 9 | #include <boost/type_traits/is_convertible.hpp> |
|---|
| 10 | #include <iostream> |
|---|
| 11 | |
|---|
| 12 | #ifndef BOOST_NO_SFINAE |
|---|
| 13 | # include <boost/utility/enable_if.hpp> |
|---|
| 14 | # include <boost/type_traits/is_same.hpp> |
|---|
| 15 | #endif |
|---|
| 16 | |
|---|
| 17 | namespace test |
|---|
| 18 | { |
|---|
| 19 | BOOST_PARAMETER_KEYWORD(keywords,name) |
|---|
| 20 | BOOST_PARAMETER_KEYWORD(keywords,value) |
|---|
| 21 | |
|---|
| 22 | using namespace boost::parameter; |
|---|
| 23 | |
|---|
| 24 | struct f_parameters |
|---|
| 25 | : parameters< |
|---|
| 26 | optional< |
|---|
| 27 | keywords::name |
|---|
| 28 | , boost::is_convertible<boost::mpl::_, std::string> |
|---|
| 29 | > |
|---|
| 30 | , optional< |
|---|
| 31 | keywords::value |
|---|
| 32 | , boost::is_convertible<boost::mpl::_, float> |
|---|
| 33 | > |
|---|
| 34 | > |
|---|
| 35 | {}; |
|---|
| 36 | |
|---|
| 37 | template <class T> struct not_implemented; |
|---|
| 38 | |
|---|
| 39 | template<class P> |
|---|
| 40 | void f_impl(P const& p) |
|---|
| 41 | { |
|---|
| 42 | std::string s = p[name | "bar"]; |
|---|
| 43 | float v = p[value | 3.f]; |
|---|
| 44 | |
|---|
| 45 | assert(s == "foo"); |
|---|
| 46 | assert(v == 3.f); |
|---|
| 47 | } |
|---|
| 48 | |
|---|
| 49 | void f() |
|---|
| 50 | { |
|---|
| 51 | f_impl(f_parameters()()); |
|---|
| 52 | } |
|---|
| 53 | |
|---|
| 54 | using boost::parameter::aux::void_; |
|---|
| 55 | |
|---|
| 56 | template<class A0> |
|---|
| 57 | void f( |
|---|
| 58 | A0 const& a0 |
|---|
| 59 | , BOOST_PARAMETER_MATCH(f_parameters, (A0), args)) |
|---|
| 60 | { |
|---|
| 61 | f_impl(args(a0)); |
|---|
| 62 | } |
|---|
| 63 | |
|---|
| 64 | template<class A0, class A1> |
|---|
| 65 | void f( |
|---|
| 66 | A0 const& a0, A1 const& a1 |
|---|
| 67 | , BOOST_PARAMETER_MATCH(f_parameters,(A0)(A1), args)) |
|---|
| 68 | { |
|---|
| 69 | f_impl(args(a0, a1)); |
|---|
| 70 | } |
|---|
| 71 | |
|---|
| 72 | #ifndef BOOST_NO_SFINAE |
|---|
| 73 | // On compilers that actually support SFINAE, add another overload |
|---|
| 74 | // that is an equally good match and can only be in the overload set |
|---|
| 75 | // when the others are not. This tests that the SFINAE is actually |
|---|
| 76 | // working. On all other compilers we're just checking that |
|---|
| 77 | // everything about SFINAE-enabled code will work, except of course |
|---|
| 78 | // the SFINAE. |
|---|
| 79 | template<class A0, class A1> |
|---|
| 80 | typename boost::enable_if<boost::is_same<int,A0>, int>::type |
|---|
| 81 | f(A0 const& a0, A1 const& a1) |
|---|
| 82 | { |
|---|
| 83 | return 0; |
|---|
| 84 | } |
|---|
| 85 | #endif |
|---|
| 86 | } // namespace test |
|---|
| 87 | |
|---|
| 88 | int main() |
|---|
| 89 | { |
|---|
| 90 | using test::name; |
|---|
| 91 | using test::value; |
|---|
| 92 | using test::f; |
|---|
| 93 | |
|---|
| 94 | f("foo"); |
|---|
| 95 | f("foo", 3.f); |
|---|
| 96 | f(value = 3.f, name = "foo"); |
|---|
| 97 | |
|---|
| 98 | #ifndef BOOST_NO_SFINAE |
|---|
| 99 | return f(3, 4); |
|---|
| 100 | #else |
|---|
| 101 | return 0; |
|---|
| 102 | #endif |
|---|
| 103 | } |
|---|
| 104 | |
|---|