File: default-arguments.cpp

package info (click to toggle)
llvm-toolchain-15 1%3A15.0.6-4
  • links: PTS, VCS
  • area: main
  • in suites: bookworm
  • size: 1,554,644 kB
  • sloc: cpp: 5,922,452; ansic: 1,012,136; asm: 674,362; python: 191,568; objc: 73,855; f90: 42,327; lisp: 31,913; pascal: 11,973; javascript: 10,144; sh: 9,421; perl: 7,447; ml: 5,527; awk: 3,523; makefile: 2,520; xml: 885; cs: 573; fortran: 567
file content (58 lines) | stat: -rw-r--r-- 2,410 bytes parent folder | download | duplicates (7)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
// RUN: %clang_cc1 -fsyntax-only -std=c++11 %s -verify
// RUN: %clang_cc1 -fsyntax-only -std=c++11 %s -verify -triple i386-windows

void defargs() {
  auto l1 = [](int i, int j = 17, int k = 18) { return i + j + k; };
  int i1 = l1(1);
  int i2 = l1(1, 2);
  int i3 = l1(1, 2, 3);
}


void defargs_errors() {
  auto l1 = [](int i,
               int j = 17,
               int k) { }; // expected-error{{missing default argument on parameter 'k'}}

  auto l2 = [](int i, int j = i) {}; // expected-error{{default argument references parameter 'i'}}

  int foo;
  auto l3 = [](int i = foo) {}; // expected-error{{default argument references local variable 'foo' of enclosing function}}
}

struct NonPOD {
  NonPOD();
  NonPOD(const NonPOD&);
  ~NonPOD();
};

struct NoDefaultCtor {
  NoDefaultCtor(const NoDefaultCtor&); // expected-note{{candidate constructor}} \
                                       // expected-note{{candidate constructor not viable: requires 1 argument, but 0 were provided}}
  ~NoDefaultCtor();
};

template<typename T>
void defargs_in_template_unused(T t) {
  auto l1 = [](const T& value = T()) { };  // expected-error{{no matching constructor for initialization of 'NoDefaultCtor'}}
  l1(t);
}

template void defargs_in_template_unused(NonPOD);
template void defargs_in_template_unused(NoDefaultCtor);  // expected-note{{in instantiation of function template specialization 'defargs_in_template_unused<NoDefaultCtor>' requested here}}

template<typename T>
void defargs_in_template_used() {
  auto l1 = [](const T& value = T()) { }; // expected-error{{no matching constructor for initialization of 'NoDefaultCtor'}} \
                                          // expected-note{{candidate function not viable: requires single argument 'value', but no arguments were provided}}
#if defined(_WIN32) && !defined(_WIN64)
                                          // expected-note@46{{conversion candidate of type 'void (*)(const NoDefaultCtor &) __attribute__((thiscall))'}}
#else
                                          // expected-note@46{{conversion candidate of type 'void (*)(const NoDefaultCtor &)'}}
#endif
  l1(); // expected-error{{no matching function for call to object of type '(lambda at }}
}

template void defargs_in_template_used<NonPOD>();
template void defargs_in_template_used<NoDefaultCtor>(); // expected-note{{in instantiation of function template specialization}}