1ba71c085SRichard Smith // RUN: %clang_cc1 -std=c++1y %s -verify
2ba71c085SRichard Smith 
__anond6f8d2030102null3bb13c9a4SRichard Smith const char *has_no_member = [x("hello")] {}.x; // expected-error {{no member named 'x'}}
4ba71c085SRichard Smith 
5bb13c9a4SRichard Smith double f;
__anond6f8d2030202null6bb13c9a4SRichard Smith auto with_float = [f(1.0f)] {
7bb13c9a4SRichard Smith   using T = decltype(f);
8bb13c9a4SRichard Smith   using T = float;
9bb13c9a4SRichard Smith };
__anond6f8d2030302null10bb13c9a4SRichard Smith auto with_float_2 = [&f(f)] { // ok, refers to outer f
11bb13c9a4SRichard Smith   using T = decltype(f);
12bb13c9a4SRichard Smith   using T = double&;
13bb13c9a4SRichard Smith };
14ba71c085SRichard Smith 
15*c79e6007SFangrui Song // Within the lambda-expression's compound-statement,
16*c79e6007SFangrui Song // the identifier in the init-capture hides any declaration
17*c79e6007SFangrui Song // of the same name in scopes enclosing the lambda-expression.
hiding()18ba71c085SRichard Smith void hiding() {
19ba71c085SRichard Smith   char c;
20ba71c085SRichard Smith   (void) [c("foo")] {
21ba71c085SRichard Smith     static_assert(sizeof(c) == sizeof(const char*), "");
22ba71c085SRichard Smith   };
23*c79e6007SFangrui Song   (void) [c("bar")] () -> decltype(c) { // outer c, not init-capture
24*c79e6007SFangrui Song     return "baz"; // expected-error {{cannot initialize}}
25ba71c085SRichard Smith   };
26ba71c085SRichard Smith }
27ba71c085SRichard Smith 
28ba71c085SRichard Smith struct ExplicitCopy {
29ba71c085SRichard Smith   ExplicitCopy(); // expected-note 2{{not viable}}
3025195541SRichard Smith   explicit ExplicitCopy(const ExplicitCopy&); // expected-note 2{{not a candidate}}
31ba71c085SRichard Smith };
__anond6f8d2030502null32ba71c085SRichard Smith auto init_kind_1 = [ec(ExplicitCopy())] {};
__anond6f8d2030602null33ba71c085SRichard Smith auto init_kind_2 = [ec = ExplicitCopy()] {}; // expected-error {{no matching constructor}}
34ba71c085SRichard Smith 
init_kind_template()35ba71c085SRichard Smith template<typename T> void init_kind_template() {
36ba71c085SRichard Smith   auto init_kind_1 = [ec(T())] {};
37ba71c085SRichard Smith   auto init_kind_2 = [ec = T()] {}; // expected-error {{no matching constructor}}
38ba71c085SRichard Smith }
39ba71c085SRichard Smith template void init_kind_template<int>();
40ba71c085SRichard Smith template void init_kind_template<ExplicitCopy>(); // expected-note {{instantiation of}}
41ba71c085SRichard Smith 
42ba71c085SRichard Smith void void_fn();
43ba71c085SRichard Smith int overload_fn();
44ba71c085SRichard Smith int overload_fn(int);
45ba71c085SRichard Smith 
__anond6f8d2030902null46ba71c085SRichard Smith auto bad_init_1 = [a()] {}; // expected-error {{expected expression}}
__anond6f8d2030a02null47ba71c085SRichard Smith auto bad_init_2 = [a(1, 2)] {}; // expected-error {{initializer for lambda capture 'a' contains multiple expressions}}
__anond6f8d2030b02null48ba71c085SRichard Smith auto bad_init_3 = [&a(void_fn())] {}; // expected-error {{cannot form a reference to 'void'}}
__anond6f8d2030c02null49bb13c9a4SRichard Smith auto bad_init_4 = [a(void_fn())] {}; // expected-error {{has incomplete type 'void'}}
__anond6f8d2030d02null50ba71c085SRichard Smith auto bad_init_5 = [a(overload_fn)] {}; // expected-error {{cannot deduce type for lambda capture 'a' from initializer of type '<overloaded function}}
__anond6f8d2030e02null5142b10572SRichard Smith auto bad_init_6 = [a{overload_fn}] {}; // expected-error {{cannot deduce type for lambda capture 'a' from initializer list}}
__anond6f8d2030f02null5242b10572SRichard Smith auto bad_init_7 = [a{{1}}] {}; // expected-error {{cannot deduce type for lambda capture 'a' from nested initializer list}}
53ba71c085SRichard Smith 
__anond6f8d2031002null54bb13c9a4SRichard Smith template<typename...T> void pack_1(T...t) { (void)[a(t...)] {}; } // expected-error {{initializer missing for lambda capture 'a'}}
55ba71c085SRichard Smith template void pack_1<>(); // expected-note {{instantiation of}}
56ba71c085SRichard Smith 
572177e455SRichard Smith // No lifetime-extension of the temporary here.
__anond6f8d2031102null582177e455SRichard Smith 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'}}
592177e455SRichard Smith 
602177e455SRichard Smith // But there is lifetime extension here.
__anond6f8d2031202null612177e455SRichard Smith auto &&a = [a(4), b = 5, &c = static_cast<const int&&>(0)] {
62ba71c085SRichard Smith   static_assert(sizeof(a) == sizeof(int), "");
63ba71c085SRichard Smith   static_assert(sizeof(b) == sizeof(int), "");
64ba71c085SRichard Smith   using T = decltype(c);
65ba71c085SRichard Smith   using T = const int &;
66ba71c085SRichard Smith };
__anond6f8d2031302null6742b10572SRichard Smith auto b = [a{0}] {}; // OK, per N3922
68ba71c085SRichard Smith 
69ba71c085SRichard Smith struct S { S(); S(S&&); };
70ba71c085SRichard Smith template<typename T> struct remove_reference { typedef T type; };
71ba71c085SRichard Smith template<typename T> struct remove_reference<T&> { typedef T type; };
move(T && t)72ba71c085SRichard Smith template<typename T> decltype(auto) move(T &&t) { return static_cast<typename remove_reference<T>::type&&>(t); }
__anond6f8d2031402null73ba71c085SRichard Smith auto s = [s(move(S()))] {};
74bb13c9a4SRichard Smith 
instantiate_test(T t)75bb13c9a4SRichard Smith template<typename T> T instantiate_test(T t) {
76bb13c9a4SRichard Smith   [x(&t)]() { *x = 1; } (); // expected-error {{assigning to 'const char *'}}
77bb13c9a4SRichard Smith   return t;
78bb13c9a4SRichard Smith }
79bb13c9a4SRichard Smith int instantiate_test_1 = instantiate_test(0);
80bb13c9a4SRichard Smith const char *instantiate_test_2 = instantiate_test("foo"); // expected-note {{here}}
81