Debian Codegraph @

gcc/testsuite/g++.dg/cpp23/explicit-obj-lambda12.C

raw
1 // P0847R7
2 // { dg-do compile { target c++23 } }
3
4 // SFINAE when the call operator for a lambda with captures is instantiated
5 // with an unrelated type.
6 // diagnose ambiguous overloads when the call operator for a captureless lambda is instantiated
7 // with an unrelated type.
8
9 // overload resolution when taking address of function
10
11 /* [expr.prim.lambda.general-5]
12
13 Given a lambda with a lambda-capture, the type of the explicit object
14 parameter, if any, of the lambda's function call operator (possibly
15 instantiated from a function call operator template) shall be either:
16
17 --(5.1) the closure type,
18 --(5.2) a class type derived from the closure type, or
19 --(5.3) a reference to a possibly cv-qualified such type. */
20
21 // The above wording is similar to [dcl.fct-15] which is handled by SFINAE,
22 // thus we also handle the following cases the same way.
23
24 // We need the 2 overloads to be ambiguous to observe substitution failure
25 // for the lambda's call operator when instantiated with an unrelated type.
26 // We accomplish this by introducing both overloads through using declarations.
27
28 struct B0 {
29 void operator()(this auto) {}
30 };
31 template<typename T>
32 struct S0 : T, B0 {
33 using B0::operator();
34 using T::operator();
35 };
36
37 void test0()
38 {
39 auto s0 = S0{[](this auto){}};
40 void (*p0)(int) = &decltype(s0)::operator(); // { dg-error {converting overloaded function '[^\n\r]+' to type '[^\n\r]+' is ambiguous} }
41
42 auto s1 = S0{[x = 42](this auto){}};
43 void (*p1)(int) = &decltype(s1)::operator(); // { dg-bogus {converting overloaded function '[^\n\r]+' to type '[^\n\r]+' is ambiguous} {Substitution failure for a captureful lambda with an unrelated xobj parameter type failed!} }
44 }
45
46 struct B1 {
47 void operator()(this auto&&) {}
48 };
49 template<typename T>
50 struct S1 : T, B1 {
51 using B1::operator();
52 using T::operator();
53 };
54
55 void test1()
56 {
57 auto s0 = S1{[](this auto&&){}};
58 void (*p0)(int&) = &decltype(s0)::operator(); // { dg-error {converting overloaded function '[^\n\r]+' to type '[^\n\r]+' is ambiguous} }
59
60 auto s1 = S1{[x = 42](this auto&&){}};
61 void (*p1)(int&) = &decltype(s1)::operator(); // { dg-bogus {converting overloaded function '[^\n\r]+' to type '[^\n\r]+' is ambiguous} {Substitution failure for a captureful lambda with an unrelated xobj parameter type failed!} }
62 }
63
64
65 struct B2 {
66 // not a template, should be taken over the lambda's call operator
67 void operator()(this int&) {}
68 };
69 template<typename T>
70 struct S2 : T, B2 {
71 using T::operator();
72 using B2::operator();
73 };
74
75 void test2()
76 {
77 auto s0 = S2{[](this auto&&){}};
78 void (*p0)(int&) = &decltype(s0)::operator(); // { dg-bogus {converting overloaded function '[^\n\r]+' to type '[^\n\r]+' is ambiguous} }
79
80 auto s1 = S2{[x = 42](this auto&&){}};
81 void (*p1)(int&) = &decltype(s1)::operator(); // { dg-bogus {converting overloaded function '[^\n\r]+' to type '[^\n\r]+' is ambiguous} }
82 }
83
84 struct B3 {
85 // must be a template so it is not taken over the lambda's call operator
86 template<typename U = void>
87 void operator()(this int&) {}
88 };
89 template<typename T>
90 struct S3 : T, B3 {
91 using B3::operator();
92 using T::operator();
93 };
94
95 void test3()
96 {
97 auto s0 = S3{[](this auto&&){}};
98 void (*p0)(int&) = &decltype(s0)::operator(); // { dg-error {converting overloaded function '[^\n\r]+' to type '[^\n\r]+' is ambiguous} }
99
100 auto s1 = S3{[x = 42](this auto&&){}};
101 void (*p1)(int&) = &decltype(s1)::operator(); // { dg-bogus {converting overloaded function '[^\n\r]+' to type '[^\n\r]+' is ambiguous} {Substitution failure for a captureful lambda with an unrelated xobj parameter type failed!} }
102 }
103