| 1 | // { dg-do run { target c++11 } } |
| 2 | // { dg-require-effective-target hosted } |
| 3 | |
| 4 | // 2005-01-15 Douglas Gregor <[email protected]> |
| 5 | // |
| 6 | // Copyright (C) 2005-2025 Free Software Foundation, Inc. |
| 7 | // |
| 8 | // This file is part of the GNU ISO C++ Library. This library is free |
| 9 | // software; you can redistribute it and/or modify it under the |
| 10 | // terms of the GNU General Public License as published by the |
| 11 | // Free Software Foundation; either version 3, or (at your option) |
| 12 | // any later version. |
| 13 | // |
| 14 | // This library is distributed in the hope that it will be useful, |
| 15 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 16 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 17 | // GNU General Public License for more details. |
| 18 | // |
| 19 | // You should have received a copy of the GNU General Public License along |
| 20 | // with this library; see the file COPYING3. If not see |
| 21 | // <http://www.gnu.org/licenses/>. |
| 22 | |
| 23 | // 20.7.15 polymorphic function object wrapper |
| 24 | #include <functional> |
| 25 | #include <testsuite_hooks.h> |
| 26 | |
| 27 | using namespace __gnu_test; |
| 28 | |
| 29 | // Operations on empty function<> objects |
| 30 | void test01() |
| 31 | { |
| 32 | using std::function; |
| 33 | using std::bad_function_call; |
| 34 | |
| 35 | // Default-construction |
| 36 | function<int(float)> f1; |
| 37 | VERIFY( ((bool)f1 == false) ); |
| 38 | VERIFY( !f1 ); |
| 39 | VERIFY( f1 == 0 ); |
| 40 | VERIFY( 0 == f1 ); |
| 41 | VERIFY( !(f1 != 0) ); |
| 42 | VERIFY( !(0 != f1) ); |
| 43 | |
| 44 | // Copy-construction |
| 45 | function<int(float)> f2(f1); |
| 46 | VERIFY( !f2 ); |
| 47 | |
| 48 | // Construct with NULL pointer |
| 49 | function<int(float)> f3(0); |
| 50 | VERIFY( !f3 ); |
| 51 | |
| 52 | // Assignment |
| 53 | f1 = f2; |
| 54 | VERIFY( !f1); |
| 55 | |
| 56 | // Assignment to NULL pointer |
| 57 | f1 = 0; |
| 58 | VERIFY( !f1 ); |
| 59 | |
| 60 | // Swap |
| 61 | swap(f1, f2); |
| 62 | VERIFY( !f1 ); |
| 63 | VERIFY( !f2 ); |
| 64 | |
| 65 | // Invocation should throw bad_function_call |
| 66 | bool thrown = false; |
| 67 | try |
| 68 | { |
| 69 | f1(3.14159f); |
| 70 | VERIFY( false ); |
| 71 | } |
| 72 | catch (const bad_function_call&) |
| 73 | { |
| 74 | thrown = true; |
| 75 | } |
| 76 | VERIFY( thrown ); |
| 77 | |
| 78 | #if __cpp_rtti |
| 79 | // target_type returns typeid(void) |
| 80 | VERIFY( f1.target_type() == typeid(void) ); |
| 81 | #endif |
| 82 | |
| 83 | // target() always returns a NULL pointer |
| 84 | VERIFY( f1.target<int (*)(float)>() == 0); |
| 85 | |
| 86 | // Check const version |
| 87 | const function<int(float)>& f1c = f1; |
| 88 | VERIFY( f1c.target<int (*)(float)>() == 0 ); |
| 89 | VERIFY( !f1c ); |
| 90 | } |
| 91 | |
| 92 | int main() |
| 93 | { |
| 94 | test01(); |
| 95 | return 0; |
| 96 | } |