p14.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // RUN: %clang_cc1 -fsyntax-only -std=c++11 %s -verify
  2. template<typename T> void capture(const T&);
  3. class NonCopyable {
  4. NonCopyable(const NonCopyable&); // expected-note 2 {{implicitly declared private here}}
  5. public:
  6. void foo() const;
  7. };
  8. class NonConstCopy {
  9. public:
  10. NonConstCopy(NonConstCopy&); // expected-note{{would lose const}}
  11. };
  12. void capture_by_copy(NonCopyable nc, NonCopyable &ncr, const NonConstCopy nco) {
  13. (void)[nc] { }; // expected-error{{capture of variable 'nc' as type 'NonCopyable' calls private copy constructor}}
  14. (void)[=] {
  15. ncr.foo(); // expected-error{{capture of variable 'ncr' as type 'NonCopyable' calls private copy constructor}}
  16. }();
  17. [nco] {}(); // expected-error{{no matching constructor for initialization of 'const NonConstCopy'}}
  18. }
  19. struct NonTrivial {
  20. NonTrivial();
  21. NonTrivial(const NonTrivial &);
  22. ~NonTrivial();
  23. };
  24. struct CopyCtorDefault {
  25. CopyCtorDefault();
  26. CopyCtorDefault(const CopyCtorDefault&, NonTrivial nt = NonTrivial());
  27. void foo() const;
  28. };
  29. void capture_with_default_args(CopyCtorDefault cct) {
  30. (void)[=] () -> void { cct.foo(); };
  31. }
  32. struct ExpectedArrayLayout {
  33. CopyCtorDefault array[3];
  34. };
  35. void capture_array() {
  36. CopyCtorDefault array[3];
  37. auto x = [=]() -> void {
  38. capture(array[0]);
  39. };
  40. static_assert(sizeof(x) == sizeof(ExpectedArrayLayout), "layout mismatch");
  41. }
  42. // Check for the expected non-static data members.
  43. struct ExpectedLayout {
  44. char a;
  45. short b;
  46. };
  47. void test_layout(char a, short b) {
  48. auto x = [=] () -> void {
  49. capture(a);
  50. capture(b);
  51. };
  52. static_assert(sizeof(x) == sizeof(ExpectedLayout), "Layout mismatch!");
  53. }
  54. struct ExpectedThisLayout {
  55. ExpectedThisLayout* a;
  56. void f() {
  57. auto x = [this]() -> void {};
  58. static_assert(sizeof(x) == sizeof(ExpectedThisLayout), "Layout mismatch!");
  59. }
  60. };
  61. struct CaptureArrayAndThis {
  62. int value;
  63. void f() {
  64. int array[3];
  65. [=]() -> int {
  66. int result = value;
  67. for (unsigned i = 0; i < 3; ++i)
  68. result += array[i];
  69. return result;
  70. }();
  71. }
  72. };
  73. namespace rdar14468891 {
  74. class X {
  75. public:
  76. virtual ~X() = 0; // expected-note{{unimplemented pure virtual method '~X' in 'X'}}
  77. };
  78. class Y : public X { };
  79. void capture(X &x) {
  80. [x]() {}(); // expected-error{{by-copy capture of value of abstract type 'rdar14468891::X'}}
  81. }
  82. }
  83. namespace rdar15560464 {
  84. struct X; // expected-note{{forward declaration of 'rdar15560464::X'}}
  85. void foo(const X& param) {
  86. auto x = ([=]() {
  87. auto& y = param; // expected-error{{by-copy capture of variable 'param' with incomplete type 'const rdar15560464::X'}}
  88. });
  89. }
  90. }