| 1 | // { dg-do run { target c++11 } } |
| 2 | // 2011-11-20 Jonathan Wakely <jwakely.gcc -at- gmail.com> |
| 3 | // |
| 4 | // Copyright (C) 2011-2025 Free Software Foundation, Inc. |
| 5 | // |
| 6 | // This file is part of the GNU ISO C++ Library. This library is free |
| 7 | // software; you can redistribute it and/or modify it under the |
| 8 | // terms of the GNU General Public License as published by the |
| 9 | // Free Software Foundation; either version 3, or (at your option) |
| 10 | // any later version. |
| 11 | // |
| 12 | // This library is distributed in the hope that it will be useful, |
| 13 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 14 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 15 | // GNU General Public License for more details. |
| 16 | // |
| 17 | // You should have received a copy of the GNU General Public License along |
| 18 | // with this library; see the file COPYING3. If not see |
| 19 | // <http://www.gnu.org/licenses/>. |
| 20 | |
| 21 | // 3.6 function object binders |
| 22 | #include <tr1/functional> |
| 23 | #include <functional> |
| 24 | #include <testsuite_hooks.h> |
| 25 | |
| 26 | // std::tr1::bind and std::bind should work together |
| 27 | |
| 28 | namespace p1 = std::placeholders; |
| 29 | namespace p2 = std::tr1::placeholders; |
| 30 | |
| 31 | using std::multiplies; |
| 32 | using std::minus; |
| 33 | |
| 34 | void test01() |
| 35 | { |
| 36 | static_assert( std::is_placeholder<decltype(p2::_2)>::value == 2, |
| 37 | "TR1 placeholder is a std placeholder" ); |
| 38 | static_assert( std::tr1::is_placeholder<decltype(p1::_1)>::value == 1, |
| 39 | "std placeholder is a TR2 placeholder" ); |
| 40 | } |
| 41 | |
| 42 | void test02() |
| 43 | { |
| 44 | auto b1 = std::bind(minus<int>(), 6, p2::_2); |
| 45 | auto b2 = std::tr1::bind(minus<int>(), 6, p1::_2); |
| 46 | |
| 47 | int five = 5; |
| 48 | int seven = 7; |
| 49 | |
| 50 | VERIFY( std::tr1::bind(multiplies<int>(), p1::_1, b1)(five, seven) == -5 ); |
| 51 | VERIFY( std::bind(multiplies<int>(), p2::_1, b2)(seven, five) == 7 ); |
| 52 | |
| 53 | VERIFY( std::tr1::bind<int>(multiplies<int>(), p1::_1, b1)(five, seven) == -5 ); |
| 54 | VERIFY( std::bind<int>(multiplies<int>(), p2::_1, b2)(seven, five) == 7 ); |
| 55 | |
| 56 | static_assert( std::is_bind_expression<decltype(b2)>::value, |
| 57 | "TR1 bind expression is a std bind expression" ); |
| 58 | static_assert( std::tr1::is_bind_expression<decltype(b1)>::value, |
| 59 | "std bind expression is a TR2 bind expression" ); |
| 60 | |
| 61 | const auto c1 = b1; |
| 62 | const auto c2 = b2; |
| 63 | |
| 64 | static_assert( std::is_bind_expression<decltype(c2)>::value, |
| 65 | "const TR1 bind expression is a std bind expression" ); |
| 66 | static_assert( std::tr1::is_bind_expression<decltype(c1)>::value, |
| 67 | "const std bind expression is a TR2 bind expression" ); |
| 68 | |
| 69 | volatile auto v1 = b1; |
| 70 | volatile auto v2 = b2; |
| 71 | |
| 72 | static_assert( std::is_bind_expression<decltype(v2)>::value, |
| 73 | "volatile TR1 bind expression is a std bind expression" ); |
| 74 | static_assert( std::tr1::is_bind_expression<decltype(v1)>::value, |
| 75 | "volatile std bind expression is a TR2 bind expression" ); |
| 76 | |
| 77 | const volatile auto cv1 = b1; |
| 78 | const volatile auto cv2 = b2; |
| 79 | |
| 80 | static_assert( std::is_bind_expression<decltype(cv2)>::value, |
| 81 | "const volatile TR1 bind expression is a std bind expression" ); |
| 82 | static_assert( std::tr1::is_bind_expression<decltype(cv1)>::value, |
| 83 | "const volatile std bind expression is a TR2 bind expression" ); |
| 84 | } |
| 85 | |
| 86 | void test03() |
| 87 | { |
| 88 | auto b1 = std::bind<int>(minus<int>(), 6, p2::_2); |
| 89 | auto b2 = std::tr1::bind<int>(minus<int>(), 6, p1::_2); |
| 90 | |
| 91 | int five = 5; |
| 92 | int seven = 7; |
| 93 | VERIFY( std::tr1::bind(multiplies<int>(), p1::_1, b1)(five, seven) == -5 ); |
| 94 | VERIFY( std::bind(multiplies<int>(), p2::_1, b2)(seven, five) == 7 ); |
| 95 | |
| 96 | VERIFY( std::tr1::bind<int>(multiplies<int>(), p1::_1, b1)(five, seven) == -5 ); |
| 97 | VERIFY( std::bind<int>(multiplies<int>(), p2::_1, b2)(seven, five) == 7 ); |
| 98 | |
| 99 | static_assert( std::is_bind_expression<decltype(b2)>::value, |
| 100 | "TR1 bind<R> expression is a std bind expression" ); |
| 101 | static_assert( std::tr1::is_bind_expression<decltype(b1)>::value, |
| 102 | "std bind<R> expression is a TR2 bind expression" ); |
| 103 | |
| 104 | const auto c1 = b1; |
| 105 | const auto c2 = b2; |
| 106 | |
| 107 | static_assert( std::is_bind_expression<decltype(c2)>::value, |
| 108 | "const TR1 bind<R> expression is a std bind expression" ); |
| 109 | static_assert( std::tr1::is_bind_expression<decltype(c1)>::value, |
| 110 | "const std bind<R> expression is a TR2 bind expression" ); |
| 111 | |
| 112 | volatile auto v1 = b1; |
| 113 | volatile auto v2 = b2; |
| 114 | |
| 115 | static_assert( std::is_bind_expression<decltype(v2)>::value, |
| 116 | "volatile TR1 bind<R> expression is a std bind expression" ); |
| 117 | static_assert( std::tr1::is_bind_expression<decltype(v1)>::value, |
| 118 | "volatile std bind<R> expression is a TR2 bind expression" ); |
| 119 | |
| 120 | const volatile auto cv1 = b1; |
| 121 | const volatile auto cv2 = b2; |
| 122 | |
| 123 | static_assert( std::is_bind_expression<decltype(cv2)>::value, |
| 124 | "const volatile TR1 bind<R> expression is a std bind expression" ); |
| 125 | static_assert( std::tr1::is_bind_expression<decltype(cv1)>::value, |
| 126 | "const volatile std bind<R> expression is a TR2 bind expression" ); |
| 127 | } |
| 128 | |
| 129 | int main() |
| 130 | { |
| 131 | test01(); |
| 132 | test02(); |
| 133 | test03(); |
| 134 | return 0; |
| 135 | } |