2
0

p11-1y.cpp 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  1. // RUN: %clang_cc1 -std=c++1y %s -verify
  2. const char *has_no_member = [x("hello")] {}.x; // expected-error {{no member named 'x'}}
  3. double f;
  4. auto with_float = [f(1.0f)] {
  5. using T = decltype(f);
  6. using T = float;
  7. };
  8. auto with_float_2 = [&f(f)] { // ok, refers to outer f
  9. using T = decltype(f);
  10. using T = double&;
  11. };
  12. // Within the lambda-expression's compound-statement,
  13. // the identifier in the init-capture hides any declaration
  14. // of the same name in scopes enclosing the lambda-expression.
  15. void hiding() {
  16. char c;
  17. (void) [c("foo")] {
  18. static_assert(sizeof(c) == sizeof(const char*), "");
  19. };
  20. (void) [c("bar")] () -> decltype(c) { // outer c, not init-capture
  21. return "baz"; // expected-error {{cannot initialize}}
  22. };
  23. }
  24. struct ExplicitCopy {
  25. ExplicitCopy(); // expected-note 2{{not viable}}
  26. explicit ExplicitCopy(const ExplicitCopy&);
  27. };
  28. auto init_kind_1 = [ec(ExplicitCopy())] {};
  29. auto init_kind_2 = [ec = ExplicitCopy()] {}; // expected-error {{no matching constructor}}
  30. template<typename T> void init_kind_template() {
  31. auto init_kind_1 = [ec(T())] {};
  32. auto init_kind_2 = [ec = T()] {}; // expected-error {{no matching constructor}}
  33. }
  34. template void init_kind_template<int>();
  35. template void init_kind_template<ExplicitCopy>(); // expected-note {{instantiation of}}
  36. void void_fn();
  37. int overload_fn();
  38. int overload_fn(int);
  39. auto bad_init_1 = [a()] {}; // expected-error {{expected expression}}
  40. auto bad_init_2 = [a(1, 2)] {}; // expected-error {{initializer for lambda capture 'a' contains multiple expressions}}
  41. auto bad_init_3 = [&a(void_fn())] {}; // expected-error {{cannot form a reference to 'void'}}
  42. auto bad_init_4 = [a(void_fn())] {}; // expected-error {{has incomplete type 'void'}}
  43. auto bad_init_5 = [a(overload_fn)] {}; // expected-error {{cannot deduce type for lambda capture 'a' from initializer of type '<overloaded function}}
  44. auto bad_init_6 = [a{overload_fn}] {}; // expected-error {{cannot deduce type for lambda capture 'a' from initializer list}} expected-warning {{will change meaning in a future version of Clang}}
  45. template<typename...T> void pack_1(T...t) { (void)[a(t...)] {}; } // expected-error {{initializer missing for lambda capture 'a'}}
  46. template void pack_1<>(); // expected-note {{instantiation of}}
  47. // FIXME: Might need lifetime extension for the temporary here.
  48. // See DR1695.
  49. auto a = [a(4), b = 5, &c = static_cast<const int&&>(0)] {
  50. static_assert(sizeof(a) == sizeof(int), "");
  51. static_assert(sizeof(b) == sizeof(int), "");
  52. using T = decltype(c);
  53. using T = const int &;
  54. };
  55. auto b = [a{0}] {}; // expected-error {{include <initializer_list>}} expected-warning {{will change meaning in a future version of Clang}}
  56. struct S { S(); S(S&&); };
  57. template<typename T> struct remove_reference { typedef T type; };
  58. template<typename T> struct remove_reference<T&> { typedef T type; };
  59. template<typename T> decltype(auto) move(T &&t) { return static_cast<typename remove_reference<T>::type&&>(t); }
  60. auto s = [s(move(S()))] {};
  61. template<typename T> T instantiate_test(T t) {
  62. [x(&t)]() { *x = 1; } (); // expected-error {{assigning to 'const char *'}}
  63. return t;
  64. }
  65. int instantiate_test_1 = instantiate_test(0);
  66. const char *instantiate_test_2 = instantiate_test("foo"); // expected-note {{here}}