1 // { dg-options "-std=gnu++11" } 2 // 2005-01-15 Douglas Gregor <dgregor@cs.indiana.edu> 3 // 4 // Copyright (C) 2005-2016 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 // 20.7.15 polymorphic function object wrapper 22 #include <functional> 23 #include <testsuite_hooks.h> 24 25 using namespace __gnu_test; 26 27 bool test __attribute__((unused)) = true; 28 29 struct secret {}; 30 31 struct noncopyable_function_object_type 32 { noncopyable_function_object_typenoncopyable_function_object_type33 noncopyable_function_object_type(secret) {} 34 operator ()noncopyable_function_object_type35 int operator()() const { return 42; } operator ()noncopyable_function_object_type36 int operator()() { return 17; } 37 38 private: 39 noncopyable_function_object_type(); 40 noncopyable_function_object_type(const noncopyable_function_object_type&); 41 void operator=(const noncopyable_function_object_type&); 42 }; 43 44 // Put reference_wrappers into function<> wrappers test06()45void test06() 46 { 47 using std::function; 48 using std::ref; 49 using std::cref; 50 51 secret password; 52 noncopyable_function_object_type x(password); 53 54 function<int()> f(ref(x)); 55 VERIFY( f ); 56 VERIFY( f() == 17 ); 57 VERIFY( f.target_type() == typeid(noncopyable_function_object_type) ); 58 VERIFY( f.target<noncopyable_function_object_type>() == &x ); 59 60 function<int()> g = f; 61 VERIFY( g ); 62 VERIFY( g() == 17 ); 63 VERIFY( g.target_type() == typeid(noncopyable_function_object_type) ); 64 VERIFY( g.target<noncopyable_function_object_type>() == &x ); 65 66 function<int()> h = cref(x); 67 VERIFY( h ); 68 VERIFY( h() == 42 ); 69 VERIFY( h.target_type() == typeid(noncopyable_function_object_type) ); 70 VERIFY( h.target<const noncopyable_function_object_type>() == &x ); 71 VERIFY( h.target<const noncopyable_function_object_type>() == &x ); 72 73 const function<int()>& hc = h; 74 VERIFY( h.target<noncopyable_function_object_type>() == 0 ); 75 VERIFY( hc.target<noncopyable_function_object_type>() == &x ); 76 } 77 main()78int main() 79 { 80 test06(); 81 return 0; 82 } 83