blob: 9b0a9ad8c25734ba4ffdf17504461c2ae04fbfb0 [file] [log] [blame]
Richard Smith3cb4c632013-04-17 16:25:20 +00001// RUN: %clang_cc1 -fsyntax-only -std=c++11 %s -verify
Douglas Gregoradb376e2012-02-14 22:28:59 +00002
3void 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
11void 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
22struct NonPOD {
23 NonPOD();
24 NonPOD(const NonPOD&);
25 ~NonPOD();
26};
27
28struct NoDefaultCtor {
Serge Pavlov73c6a242015-08-23 10:22:28 +000029 NoDefaultCtor(const NoDefaultCtor&); // expected-note{{candidate constructor}} \
30 // expected-note{{candidate constructor not viable: requires 1 argument, but 0 were provided}}
Douglas Gregoradb376e2012-02-14 22:28:59 +000031 ~NoDefaultCtor();
32};
33
34template<typename T>
35void defargs_in_template_unused(T t) {
Serge Pavlov73c6a242015-08-23 10:22:28 +000036 auto l1 = [](const T& value = T()) { }; // expected-error{{no matching constructor for initialization of 'NoDefaultCtor'}}
Douglas Gregoradb376e2012-02-14 22:28:59 +000037 l1(t);
38}
39
40template void defargs_in_template_unused(NonPOD);
Serge Pavlov73c6a242015-08-23 10:22:28 +000041template void defargs_in_template_unused(NoDefaultCtor); // expected-note{{in instantiation of function template specialization 'defargs_in_template_unused<NoDefaultCtor>' requested here}}
Douglas Gregoradb376e2012-02-14 22:28:59 +000042
43template<typename T>
44void defargs_in_template_used() {
Serge Pavlov73c6a242015-08-23 10:22:28 +000045 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 Gregoradb376e2012-02-14 22:28:59 +000049}
50
51template void defargs_in_template_used<NonPOD>();
52template void defargs_in_template_used<NoDefaultCtor>(); // expected-note{{in instantiation of function template specialization}}
53