p5.cpp 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263
  1. // RUN: %clang_cc1 -std=c++11 %s -Winvalid-noreturn -verify
  2. // An attribute-specifier-seq in a lambda-declarator appertains to the
  3. // type of the corresponding function call operator.
  4. void test_attributes() {
  5. auto nrl = [](int x) -> int { if (x > 0) return x; }; // expected-warning{{control may reach end of non-void lambda}}
  6. // FIXME: GCC accepts the [[gnu::noreturn]] attribute here.
  7. auto nrl2 = []() [[gnu::noreturn]] { return; }; // expected-warning{{attribute 'noreturn' ignored}}
  8. }
  9. template<typename T>
  10. struct bogus_override_if_virtual : public T {
  11. bogus_override_if_virtual() : T(*(T*)0) { }
  12. int operator()() const;
  13. };
  14. void test_quals() {
  15. // This function call operator is declared const (9.3.1) if and only
  16. // if the lambda- expression's parameter-declaration-clause is not
  17. // followed by mutable.
  18. auto l = [=](){}; // expected-note{{method is not marked volatile}}
  19. const decltype(l) lc = l;
  20. l();
  21. lc();
  22. auto ml = [=]() mutable{}; // expected-note{{method is not marked const}} \
  23. // expected-note{{method is not marked volatile}}
  24. const decltype(ml) mlc = ml;
  25. ml();
  26. mlc(); // expected-error{{no matching function for call to object of type}}
  27. // It is neither virtual nor declared volatile.
  28. volatile decltype(l) lv = l;
  29. volatile decltype(ml) mlv = ml;
  30. lv(); // expected-error{{no matching function for call to object of type}}
  31. mlv(); // expected-error{{no matching function for call to object of type}}
  32. bogus_override_if_virtual<decltype(l)> bogus;
  33. }
  34. // Core issue 974: default arguments (8.3.6) may be specified in the
  35. // parameter-declaration-clause of a lambda-declarator.
  36. int test_default_args() {
  37. return [](int i = 5, int j = 17) { return i+j;}(5, 6);
  38. }
  39. // Any exception-specification specified on a lambda-expression
  40. // applies to the corresponding function call operator.
  41. void test_exception_spec() {
  42. auto tl1 = []() throw(int) {};
  43. auto tl2 = []() {};
  44. static_assert(!noexcept(tl1()), "lambda can throw");
  45. static_assert(!noexcept(tl2()), "lambda can throw");
  46. auto ntl1 = []() throw() {};
  47. auto ntl2 = []() noexcept(true) {};
  48. auto ntl3 = []() noexcept {};
  49. static_assert(noexcept(ntl1()), "lambda cannot throw");
  50. static_assert(noexcept(ntl2()), "lambda cannot throw");
  51. static_assert(noexcept(ntl3()), "lambda cannot throw");
  52. }