Richard Smith | 3cb4c63 | 2013-04-17 16:25:20 +0000 | [diff] [blame] | 1 | // RUN: %clang_cc1 -fsyntax-only -std=c++11 %s -verify |
Douglas Gregor | adb376e | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 2 | |
| 3 | void defargs() { |
| 4 | auto l1 = [](int i, int j = 17, int k = 18) { return i + j + k; }; |
| 5 | int i1 = l1(1); |
| 6 | int i2 = l1(1, 2); |
| 7 | int i3 = l1(1, 2, 3); |
| 8 | } |
| 9 | |
| 10 | |
| 11 | void defargs_errors() { |
| 12 | auto l1 = [](int i, |
| 13 | int j = 17, |
| 14 | int k) { }; // expected-error{{missing default argument on parameter 'k'}} |
| 15 | |
| 16 | auto l2 = [](int i, int j = i) {}; // expected-error{{default argument references parameter 'i'}} |
| 17 | |
| 18 | int foo; |
| 19 | auto l3 = [](int i = foo) {}; // expected-error{{default argument references local variable 'foo' of enclosing function}} |
| 20 | } |
| 21 | |
| 22 | struct NonPOD { |
| 23 | NonPOD(); |
| 24 | NonPOD(const NonPOD&); |
| 25 | ~NonPOD(); |
| 26 | }; |
| 27 | |
| 28 | struct NoDefaultCtor { |
Serge Pavlov | 73c6a24 | 2015-08-23 10:22:28 +0000 | [diff] [blame] | 29 | NoDefaultCtor(const NoDefaultCtor&); // expected-note{{candidate constructor}} \ |
| 30 | // expected-note{{candidate constructor not viable: requires 1 argument, but 0 were provided}} |
Douglas Gregor | adb376e | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 31 | ~NoDefaultCtor(); |
| 32 | }; |
| 33 | |
| 34 | template<typename T> |
| 35 | void defargs_in_template_unused(T t) { |
Serge Pavlov | 73c6a24 | 2015-08-23 10:22:28 +0000 | [diff] [blame] | 36 | auto l1 = [](const T& value = T()) { }; // expected-error{{no matching constructor for initialization of 'NoDefaultCtor'}} |
Douglas Gregor | adb376e | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 37 | l1(t); |
| 38 | } |
| 39 | |
| 40 | template void defargs_in_template_unused(NonPOD); |
Serge Pavlov | 73c6a24 | 2015-08-23 10:22:28 +0000 | [diff] [blame] | 41 | template void defargs_in_template_unused(NoDefaultCtor); // expected-note{{in instantiation of function template specialization 'defargs_in_template_unused<NoDefaultCtor>' requested here}} |
Douglas Gregor | adb376e | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 42 | |
| 43 | template<typename T> |
| 44 | void defargs_in_template_used() { |
Serge Pavlov | 73c6a24 | 2015-08-23 10:22:28 +0000 | [diff] [blame] | 45 | auto l1 = [](const T& value = T()) { }; // expected-error{{no matching constructor for initialization of 'NoDefaultCtor'}} \ |
| 46 | // expected-note{{candidate function not viable: requires single argument 'value', but no arguments were provided}} \ |
| 47 | // expected-note{{conversion candidate of type 'void (*)(const NoDefaultCtor &)'}} |
| 48 | l1(); // expected-error{{no matching function for call to object of type '(lambda at }} |
Douglas Gregor | adb376e | 2012-02-14 22:28:59 +0000 | [diff] [blame] | 49 | } |
| 50 | |
| 51 | template void defargs_in_template_used<NonPOD>(); |
| 52 | template void defargs_in_template_used<NoDefaultCtor>(); // expected-note{{in instantiation of function template specialization}} |
| 53 | |