p5-generic-lambda-1y.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s -std=c++1y -DCXX1Y
  2. namespace test_factorial {
  3. auto Fact = [](auto Self, unsigned n) -> unsigned {
  4. return !n ? 1 : Self(Self, n - 1) * n;
  5. };
  6. auto six = Fact(Fact, 3);
  7. }
  8. namespace overload_generic_lambda {
  9. template <class F1, class F2> struct overload : F1, F2 {
  10. using F1::operator();
  11. using F2::operator();
  12. overload(F1 f1, F2 f2) : F1(f1), F2(f2) { }
  13. };
  14. auto NumParams = [](auto Self, auto h, auto ... rest) -> unsigned {
  15. return 1 + Self(Self, rest...);
  16. };
  17. auto Base = [](auto Self, auto h) -> unsigned {
  18. return 1;
  19. };
  20. overload<decltype(Base), decltype(NumParams)> O(Base, NumParams);
  21. int num_params = O(O, 5, 3, "abc", 3.14, 'a');
  22. }
  23. namespace overload_generic_lambda_return_type_deduction {
  24. template <class F1, class F2> struct overload : F1, F2 {
  25. using F1::operator();
  26. using F2::operator();
  27. overload(F1 f1, F2 f2) : F1(f1), F2(f2) { }
  28. };
  29. auto NumParams = [](auto Self, auto h, auto ... rest) {
  30. return 1 + Self(Self, rest...);
  31. };
  32. auto Base = [](auto Self, auto h) {
  33. return 1;
  34. };
  35. overload<decltype(Base), decltype(NumParams)> O(Base, NumParams);
  36. int num_params = O(O, 5, 3, "abc", 3.14, 'a');
  37. }
  38. namespace test_standard_p5 {
  39. // FIXME: This test should eventually compile without an explicit trailing return type
  40. auto glambda = [](auto a, auto&& b) ->bool { return a < b; };
  41. bool b = glambda(3, 3.14); // OK
  42. }
  43. namespace test_deduction_failure {
  44. int test() {
  45. auto g = [](auto *a) { //expected-note{{candidate template ignored}}
  46. return a;
  47. };
  48. struct X { };
  49. X *x;
  50. g(x);
  51. g(3); //expected-error{{no matching function}}
  52. return 0;
  53. }
  54. }
  55. namespace test_instantiation_or_sfinae_failure {
  56. int test2() {
  57. {
  58. auto L = [](auto *a) {
  59. return (*a)(a); }; //expected-error{{called object type 'double' is not a function}}
  60. double d;
  61. L(&d); //expected-note{{in instantiation of}}
  62. auto M = [](auto b) { return b; };
  63. L(&M); // ok
  64. }
  65. {
  66. auto L = [](auto *a) ->decltype (a->foo()) { //expected-note2{{candidate template ignored:}}
  67. return (*a)(a); };
  68. double d;
  69. L(&d); //expected-error{{no matching function for call}}
  70. auto M = [](auto b) { return b; };
  71. L(&M); //expected-error{{no matching function for call}}
  72. }
  73. return 0;
  74. }
  75. }
  76. namespace test_misc {
  77. auto GL = [](auto a, decltype(a) b) //expected-note{{candidate function}}
  78. -> int { return a + b; };
  79. void test() {
  80. struct X { };
  81. GL(3, X{}); //expected-error{{no matching function}}
  82. }
  83. void test2() {
  84. auto l = [](auto *a) -> int {
  85. (*a)(a); return 0; }; //expected-error{{called object type 'double' is not a function}}
  86. l(&l);
  87. double d;
  88. l(&d); //expected-note{{in instantiation of}}
  89. }
  90. }
  91. namespace nested_lambdas {
  92. int test() {
  93. auto L = [](auto a) {
  94. return [=](auto b) {
  95. return a + b;
  96. };
  97. };
  98. }
  99. auto get_lambda() {
  100. return [](auto a) {
  101. return a;
  102. };
  103. };
  104. int test2() {
  105. auto L = get_lambda();
  106. L(3);
  107. }
  108. }