Run DCE after a LoopFlatten test to reduce spurious output [nfc]
[llvm-project.git] / clang / test / CXX / expr / expr.prim / expr.prim.lambda / p11-1y.cpp
blobed36a33850ce45a6ee194cd6f1f8ec1c03961f8b
1 // RUN: %clang_cc1 -std=c++1y %s -verify
3 const char *has_no_member = [x("hello")] {}.x; // expected-error {{no member named 'x'}}
5 double f;
6 auto with_float = [f(1.0f)] {
7 using T = decltype(f);
8 using T = float;
9 };
10 auto with_float_2 = [&f(f)] { // ok, refers to outer f
11 using T = decltype(f);
12 using T = double&;
15 // Within the lambda-expression the identifier in the init-capture
16 // hides any declaration of the same name in scopes enclosing
17 // the lambda-expression.
18 void hiding() {
19 char c;
20 (void) [c("foo")] {
21 static_assert(sizeof(c) == sizeof(const char*), "");
23 (void)[c("bar")]()->decltype(c) { // inner c
24 return "baz";
28 struct ExplicitCopy {
29 ExplicitCopy(); // expected-note 2{{not viable}}
30 explicit ExplicitCopy(const ExplicitCopy&); // expected-note 2{{not a candidate}}
32 auto init_kind_1 = [ec(ExplicitCopy())] {};
33 auto init_kind_2 = [ec = ExplicitCopy()] {}; // expected-error {{no matching constructor}}
35 template<typename T> void init_kind_template() {
36 auto init_kind_1 = [ec(T())] {};
37 auto init_kind_2 = [ec = T()] {}; // expected-error {{no matching constructor}}
38 // expected-note@-1 {{while substituting into a lambda expression here}}
40 template void init_kind_template<int>();
41 template void init_kind_template<ExplicitCopy>(); // expected-note {{instantiation of}}
43 void void_fn();
44 int overload_fn();
45 int overload_fn(int);
47 auto bad_init_1 = [a()] {}; // expected-error {{expected expression}}
48 auto bad_init_2 = [a(1, 2)] {}; // expected-error {{initializer for lambda capture 'a' contains multiple expressions}}
49 auto bad_init_3 = [&a(void_fn())] {}; // expected-error {{cannot form a reference to 'void'}}
50 auto bad_init_4 = [a(void_fn())] {}; // expected-error {{has incomplete type 'void'}}
51 auto bad_init_5 = [a(overload_fn)] {}; // expected-error {{cannot deduce type for lambda capture 'a' from initializer of type '<overloaded function}}
52 auto bad_init_6 = [a{overload_fn}] {}; // expected-error {{cannot deduce type for lambda capture 'a' from initializer list}}
53 auto bad_init_7 = [a{{1}}] {}; // expected-error {{cannot deduce type for lambda capture 'a' from nested initializer list}}
55 template<typename...T> void pack_1(T...t) { (void)[a(t...)] {}; } // expected-error {{initializer missing for lambda capture 'a'}}
56 // expected-note@-1 {{while substituting into a lambda expression here}}
57 template void pack_1<>(); // expected-note {{instantiation of}}
59 // No lifetime-extension of the temporary here.
60 auto a_copy = [&c = static_cast<const int&&>(0)] {}; // expected-warning {{temporary whose address is used as value of local variable 'a_copy' will be destroyed at the end of the full-expression}} expected-note {{via initialization of lambda capture 'c'}}
62 // But there is lifetime extension here.
63 auto &&a = [a(4), b = 5, &c = static_cast<const int&&>(0)] {
64 static_assert(sizeof(a) == sizeof(int), "");
65 static_assert(sizeof(b) == sizeof(int), "");
66 using T = decltype(c);
67 using T = const int &;
69 auto b = [a{0}] {}; // OK, per N3922
71 struct S { S(); S(S&&); };
72 template<typename T> struct remove_reference { typedef T type; };
73 template<typename T> struct remove_reference<T&> { typedef T type; };
74 template<typename T> decltype(auto) move(T &&t) { return static_cast<typename remove_reference<T>::type&&>(t); }
75 auto s = [s(move(S()))] {};
77 template<typename T> T instantiate_test(T t) {
78 [x(&t)]() { *x = 1; } (); // expected-error {{assigning to 'const char *'}}
79 // expected-note@-1 {{while substituting into a lambda expression here}}
80 return t;
82 int instantiate_test_1 = instantiate_test(0);
83 const char *instantiate_test_2 = instantiate_test("foo"); // expected-note {{here}}