Clang Project

clang_source_code/test/CXX/expr/expr.prim/expr.prim.lambda/default-arguments.cpp
1// RUN: %clang_cc1 -fsyntax-only -std=c++11 %s -verify
2
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 {
29  NoDefaultCtor(const NoDefaultCtor&); // expected-note{{candidate constructor}} \
30                                       // expected-note{{candidate constructor not viable: requires 1 argument, but 0 were provided}}
31  ~NoDefaultCtor();
32};
33
34template<typename T>
35void defargs_in_template_unused(T t) {
36  auto l1 = [](const T& value = T()) { };  // expected-error{{no matching constructor for initialization of 'NoDefaultCtor'}}
37  l1(t);
38}
39
40template void defargs_in_template_unused(NonPOD);
41template void defargs_in_template_unused(NoDefaultCtor);  // expected-note{{in instantiation of function template specialization 'defargs_in_template_unused<NoDefaultCtor>' requested here}}
42
43template<typename T>
44void defargs_in_template_used() {
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 }}
49}
50
51template void defargs_in_template_used<NonPOD>();
52template void defargs_in_template_used<NoDefaultCtor>(); // expected-note{{in instantiation of function template specialization}}
53
54