1 // Copyright 2011 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This is a "No Compile Test" suite.
6 // http://dev.chromium.org/developers/testing/no-compile-tests
8 #include "base/functional/callback.h"
15 class Child : Parent {
18 #if defined(NCTEST_EQUALS_REQUIRES_SAMETYPE) // [r"fatal error: invalid operands to binary expression \('RepeatingCallback<void \(\)>' and 'RepeatingCallback<int \(\)>'\)"]
20 // Attempting to call comparison function on two callbacks of different type.
22 // This should be a compile time failure because each callback type should be
23 // considered distinct.
25 RepeatingCallback<void()> c1;
26 RepeatingCallback<int()> c2;
30 #elif defined(NCTEST_CONSTRUCTION_FROM_SUBTYPE) // [r"fatal error: no viable conversion from 'RepeatingCallback<Parent \(\)>' to 'RepeatingCallback<Child \(\)>'"]
32 // Construction of RepeatingCallback<A> from RepeatingCallback<B> if A is
35 // While this is technically safe, most people aren't used to it when coding
36 // C++ so if this is happening, it is almost certainly an error.
38 RepeatingCallback<Parent()> cb_a;
39 RepeatingCallback<Child()> cb_b = cb_a;
42 #elif defined(NCTEST_ASSIGNMENT_FROM_SUBTYPE) // [r"fatal error: no viable overloaded '='"]
44 // Assignment of RepeatingCallback<A> from RepeatingCallback<B> if A is
45 // supertype of B. See explanation for NCTEST_CONSTRUCTION_FROM_SUBTYPE.
47 RepeatingCallback<Parent()> cb_a;
48 RepeatingCallback<Child()> cb_b;
52 #elif defined(NCTEST_ONCE_THEN_MISMATCH) // [r"static assertion failed due to requirement '.+': \|then\| callback's parameter must be constructible from return type of \|this\|\."]
54 // Calling Then() with a callback that can't receive the original
55 // callback's return type. Here we would pass `int*` to `float*`.
57 OnceCallback<int*()> original;
58 OnceCallback<void(float*)> then;
59 std::move(original).Then(std::move(then));
62 #elif defined(NCTEST_ONCE_THEN_MISMATCH_VOID_RESULT) // [r"fatal error: static assertion failed due to requirement '.+': \|then\| callback cannot accept parameters if \|this\| has a void return type\."]
64 // Calling Then() with a callback that can't receive the original
65 // callback's return type. Here we would pass `void` to `float`.
67 OnceCallback<void()> original;
68 OnceCallback<void(float)> then;
69 std::move(original).Then(std::move(then));
72 #elif defined(NCTEST_ONCE_THEN_MISMATCH_VOID_PARAM) // [r"fatal error: static assertion failed due to requirement '.+': \|then\| callback must accept exactly one parameter if \|this\| has a non-void return type\."]
74 // Calling Then() with a callback that can't receive the original
75 // callback's return type. Here we would pass `int` to `void`.
77 OnceCallback<int()> original;
78 OnceCallback<void()> then;
79 std::move(original).Then(std::move(then));
82 #elif defined(NCTEST_REPEATINGRVALUE_THEN_MISMATCH) // [r"static assertion failed due to requirement '.+': \|then\| callback's parameter must be constructible from return type of \|this\|\."]
84 // Calling Then() with a callback that can't receive the original
85 // callback's return type. Here we would pass `int*` to `float*`.
87 RepeatingCallback<int*()> original;
88 RepeatingCallback<void(float*)> then;
89 std::move(original).Then(std::move(then));
92 #elif defined(NCTEST_REPEATINGRVALUE_THEN_MISMATCH_VOID_RESULT) // [r"fatal error: static assertion failed due to requirement '.+': \|then\| callback cannot accept parameters if \|this\| has a void return type\."]
94 // Calling Then() with a callback that can't receive the original
95 // callback's return type. Here we would pass `void` to `float`.
97 RepeatingCallback<void()> original;
98 RepeatingCallback<void(float)> then;
99 std::move(original).Then(std::move(then));
102 #elif defined(NCTEST_REPEATINGRVALUE_THEN_MISMATCH_VOID_PARAM) // [r"fatal error: static assertion failed due to requirement '.+': \|then\| callback must accept exactly one parameter if \|this\| has a non-void return type\."]
104 // Calling Then() with a callback that can't receive the original
105 // callback's return type. Here we would pass `int` to `void`.
107 RepeatingCallback<int()> original;
108 RepeatingCallback<void()> then;
109 std::move(original).Then(std::move(then));
112 #elif defined(NCTEST_REPEATINGLVALUE_THEN_MISMATCH) // [r"static assertion failed due to requirement '.+': \|then\| callback's parameter must be constructible from return type of \|this\|\."]
114 // Calling Then() with a callback that can't receive the original
115 // callback's return type. Here we would pass `int*` to `float*`.
117 RepeatingCallback<int*()> original;
118 RepeatingCallback<void(float*)> then;
122 #elif defined(NCTEST_REPEATINGLVALUE_THEN_MISMATCH_VOID_RESULT) // [r"fatal error: static assertion failed due to requirement '.+': \|then\| callback cannot accept parameters if \|this\| has a void return type\."]
124 // Calling Then() with a callback that can't receive the original
125 // callback's return type. Here we would pass `void` to `float`.
127 RepeatingCallback<void()> original;
128 RepeatingCallback<void(float)> then;
132 #elif defined(NCTEST_REPEATINGLVALUE_THEN_MISMATCH_VOID_PARAM) // [r"fatal error: static assertion failed due to requirement '.+': \|then\| callback must accept exactly one parameter if \|this\| has a non-void return type\."]
134 // Calling Then() with a callback that can't receive the original
135 // callback's return type. Here we would pass `int` to `void`.
137 RepeatingCallback<int()> original;
138 RepeatingCallback<void()> then;