1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // <functional>
10 
11 // class function<R(ArgTypes...)>
12 
13 // template<class F> function(F);
14 
15 // Allow incomplete argument types in the __is_callable check
16 
17 // This test runs in C++03, but we have deprecated using std::function in C++03.
18 // ADDITIONAL_COMPILE_FLAGS: -D_LIBCPP_DISABLE_DEPRECATION_WARNINGS -D_LIBCPP_ENABLE_CXX03_FUNCTION
19 
20 #include <functional>
21 #include <cassert>
22 
23 #include "test_macros.h"
24 
25 struct X{
26     typedef std::function<void(X&)> callback_type;
27     virtual ~X() {}
28 private:
29     callback_type _cb;
30 };
31 
32 struct IncompleteReturnType {
33   std::function<IncompleteReturnType ()> fn;
34 };
35 
36 
37 int called = 0;
38 IncompleteReturnType test_fn() {
39   ++called;
40   IncompleteReturnType I;
41   return I;
42 }
43 
44 // See llvm.org/PR34298
45 void test_pr34298()
46 {
47   static_assert(std::is_copy_constructible<IncompleteReturnType>::value, "");
48   static_assert(std::is_copy_assignable<IncompleteReturnType>::value, "");
49   {
50     IncompleteReturnType X;
51     X.fn = test_fn;
52     const IncompleteReturnType& CX = X;
53     IncompleteReturnType X2 = CX;
54     assert(X2.fn);
55     assert(called == 0);
56     X2.fn();
57     assert(called == 1);
58   }
59   {
60     IncompleteReturnType Empty;
61     IncompleteReturnType X2 = Empty;
62     assert(!X2.fn);
63   }
64 }
65 
66 int main(int, char**) {
67   test_pr34298();
68 
69   return 0;
70 }
71