p3-0x.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  1. // RUN: %clang_cc1 -fsyntax-only -std=c++11 %s -verify
  2. struct A {
  3. int &f(int*);
  4. float &f(int*) const noexcept;
  5. int *ptr;
  6. auto g1() noexcept(noexcept(f(ptr))) -> decltype(f(this->ptr));
  7. auto g2() const noexcept(noexcept(f((*this).ptr))) -> decltype(f(ptr));
  8. };
  9. void testA(A &a) {
  10. int &ir = a.g1();
  11. float &fr = a.g2();
  12. static_assert(!noexcept(a.g1()), "exception-specification failure");
  13. static_assert(noexcept(a.g2()), "exception-specification failure");
  14. }
  15. struct B {
  16. char g();
  17. template<class T> auto f(T t) -> decltype(t + g())
  18. { return t + g(); }
  19. };
  20. template auto B::f(int t) -> decltype(t + g());
  21. template<typename T>
  22. struct C {
  23. int &f(T*);
  24. float &f(T*) const noexcept;
  25. T* ptr;
  26. auto g1() noexcept(noexcept(f(ptr))) -> decltype(f(ptr));
  27. auto g2() const noexcept(noexcept(f(((this))->ptr))) -> decltype(f(ptr));
  28. auto g3() noexcept(noexcept(f(this->ptr))) -> decltype(f((*this).ptr));
  29. auto g4() const noexcept(noexcept(f(((this))->ptr))) -> decltype(f(this->ptr));
  30. auto g5() noexcept(noexcept(this->f(ptr))) -> decltype(this->f(ptr));
  31. auto g6() const noexcept(noexcept(this->f(((this))->ptr))) -> decltype(this->f(ptr));
  32. auto g7() noexcept(noexcept(this->f(this->ptr))) -> decltype(this->f((*this).ptr));
  33. auto g8() const noexcept(noexcept(this->f(((this))->ptr))) -> decltype(this->f(this->ptr));
  34. };
  35. void test_C(C<int> ci) {
  36. int &ir = ci.g1();
  37. float &fr = ci.g2();
  38. int &ir2 = ci.g3();
  39. float &fr2 = ci.g4();
  40. int &ir3 = ci.g5();
  41. float &fr3 = ci.g6();
  42. int &ir4 = ci.g7();
  43. float &fr4 = ci.g8();
  44. static_assert(!noexcept(ci.g1()), "exception-specification failure");
  45. static_assert(noexcept(ci.g2()), "exception-specification failure");
  46. static_assert(!noexcept(ci.g3()), "exception-specification failure");
  47. static_assert(noexcept(ci.g4()), "exception-specification failure");
  48. static_assert(!noexcept(ci.g5()), "exception-specification failure");
  49. static_assert(noexcept(ci.g6()), "exception-specification failure");
  50. static_assert(!noexcept(ci.g7()), "exception-specification failure");
  51. static_assert(noexcept(ci.g8()), "exception-specification failure");
  52. }
  53. namespace PR14263 {
  54. template<typename T> struct X {
  55. void f();
  56. T f() const;
  57. auto g() -> decltype(this->f()) { return f(); }
  58. auto g() const -> decltype(this->f()) { return f(); }
  59. };
  60. template struct X<int>;
  61. }
  62. namespace PR10036 {
  63. template <class I>
  64. void
  65. iter_swap(I x, I y) noexcept;
  66. template <class T>
  67. class A
  68. {
  69. T t_;
  70. public:
  71. void swap(A& a) noexcept(noexcept(iter_swap(&t_, &a.t_)));
  72. };
  73. void test() {
  74. A<int> i, j;
  75. i.swap(j);
  76. }
  77. }
  78. namespace PR15290 {
  79. template<typename T>
  80. class A {
  81. T v_;
  82. friend int add_to_v(A &t) noexcept(noexcept(v_ + 42))
  83. {
  84. return t.v_ + 42;
  85. }
  86. };
  87. void f()
  88. {
  89. A<int> t;
  90. add_to_v(t);
  91. }
  92. }
  93. namespace Static {
  94. struct X1 {
  95. int m;
  96. // FIXME: This should be accepted.
  97. static auto f() -> decltype(m); // expected-error{{'this' cannot be implicitly used in a static member function declaration}}
  98. static auto g() -> decltype(this->m); // expected-error{{'this' cannot be used in a static member function declaration}}
  99. static int h();
  100. static int i() noexcept(noexcept(m + 2)); // expected-error{{'this' cannot be implicitly used in a static member function declaration}}
  101. };
  102. auto X1::h() -> decltype(m) { return 0; } // expected-error{{'this' cannot be implicitly used in a static member function declaration}}
  103. template<typename T>
  104. struct X2 {
  105. int m;
  106. T f(T*);
  107. static T f(int);
  108. auto g(T x) -> decltype(f(x)) { return 0; }
  109. };
  110. void test_X2() {
  111. X2<int>().g(0);
  112. }
  113. }
  114. namespace PR12564 {
  115. struct Base {
  116. void bar(Base&) {}
  117. };
  118. struct Derived : Base {
  119. void foo(Derived& d) noexcept(noexcept(d.bar(d))) {}
  120. };
  121. }
  122. namespace rdar13473493 {
  123. template <typename F>
  124. class wrap
  125. {
  126. public:
  127. template <typename... Args>
  128. auto operator()(Args&&... args) const -> decltype(wrapped(args...)) // expected-note{{candidate template ignored: substitution failure [with Args = <int>]: use of undeclared identifier 'wrapped'}}
  129. {
  130. return wrapped(args...);
  131. }
  132. private:
  133. F wrapped;
  134. };
  135. void test(wrap<int (*)(int)> w) {
  136. w(5); // expected-error{{no matching function for call to object of type 'wrap<int (*)(int)>'}}
  137. }
  138. }