templates.cpp 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. // RUN: %clang_cc1 -fsyntax-only -std=c++11 -Winvalid-noreturn %s -verify
  2. template<typename T>
  3. void test_attributes() {
  4. // FIXME: GCC accepts [[gnu::noreturn]] here.
  5. auto nrl = []() [[gnu::noreturn]] {}; // expected-warning{{attribute 'noreturn' ignored}}
  6. }
  7. template void test_attributes<int>();
  8. template<typename T>
  9. void call_with_zero() {
  10. [](T *ptr) -> T& { return *ptr; }(0);
  11. }
  12. template void call_with_zero<int>();
  13. template<typename T>
  14. T captures(T x, T y) {
  15. auto lambda = [=, &y] () -> T {
  16. T i = x;
  17. return i + y;
  18. };
  19. return lambda();
  20. }
  21. struct X {
  22. X(const X&);
  23. };
  24. X operator+(X, X);
  25. X operator-(X, X);
  26. template int captures(int, int);
  27. template X captures(X, X);
  28. template<typename T>
  29. int infer_result(T x, T y) {
  30. auto lambda = [=](bool b) { return x + y; };
  31. return lambda(true); // expected-error{{no viable conversion from 'X' to 'int'}}
  32. }
  33. template int infer_result(int, int);
  34. template int infer_result(X, X); // expected-note{{in instantiation of function template specialization 'infer_result<X>' requested here}}
  35. // Make sure that lambda's operator() can be used from templates.
  36. template<typename F>
  37. void accept_lambda(F f) {
  38. f(1);
  39. }
  40. template<typename T>
  41. void pass_lambda(T x) {
  42. accept_lambda([&x](T y) { return x + y; });
  43. }
  44. template void pass_lambda(int);
  45. namespace std {
  46. class type_info;
  47. }
  48. namespace p2 {
  49. struct P {
  50. virtual ~P();
  51. };
  52. template<typename T>
  53. struct Boom {
  54. Boom(const Boom&) {
  55. T* x = 1; // expected-error{{cannot initialize a variable of type 'float *' with an rvalue of type 'int'}}
  56. }
  57. void tickle() const;
  58. };
  59. template<typename R, typename T>
  60. void odr_used(R &r, Boom<T> boom) {
  61. const std::type_info &ti
  62. = typeid([=,&r] () -> R& { // expected-error{{lambda expression in an unevaluated operand}}
  63. boom.tickle();
  64. return r;
  65. }());
  66. }
  67. template void odr_used(int&, Boom<int>); // expected-note{{in instantiation of function template specialization}}
  68. template<typename R, typename T>
  69. void odr_used2(R &r, Boom<T> boom) {
  70. const std::type_info &ti
  71. = typeid([=,&r] () -> R& {
  72. boom.tickle(); // expected-note{{in instantiation of member function}}
  73. return r;
  74. }());
  75. }
  76. template void odr_used2(P&, Boom<float>);
  77. }
  78. namespace p5 {
  79. struct NonConstCopy {
  80. NonConstCopy(const NonConstCopy&) = delete;
  81. NonConstCopy(NonConstCopy&);
  82. };
  83. template<typename T>
  84. void double_capture(T &nc) {
  85. [=] () mutable {
  86. [=] () mutable {
  87. T nc2(nc);
  88. }();
  89. }();
  90. }
  91. template void double_capture(NonConstCopy&);
  92. }
  93. namespace NonLocalLambdaInstantation {
  94. template<typename T>
  95. struct X {
  96. static int value;
  97. };
  98. template<typename T>
  99. int X<T>::value = []{ return T(); }(); // expected-error{{cannot initialize a variable of type 'int' with an rvalue of type 'int *'}}
  100. template int X<int>::value;
  101. template int X<float>::value;
  102. template int X<int*>::value; // expected-note{{in instantiation of static data member }}
  103. template<typename T>
  104. void defaults(int x = []{ return T(); }()) { }; // expected-error{{cannot initialize a parameter of type 'int' with an rvalue of type 'int *'}} \
  105. // expected-note{{passing argument to parameter 'x' here}}
  106. void call_defaults() {
  107. defaults<int>();
  108. defaults<float>();
  109. defaults<int*>(); // expected-note{{in instantiation of default function argument expression for 'defaults<int *>' required here}}
  110. }
  111. template<typename T>
  112. struct X2 { // expected-note{{in instantiation of default member initializer 'NonLocalLambdaInstantation::X2<int *>::x' requested here}}
  113. int x = []{ return T(); }(); // expected-error{{cannot initialize a member subobject of type 'int' with an rvalue of type 'int *'}}
  114. };
  115. X2<int> x2i;
  116. X2<float> x2f;
  117. X2<int*> x2ip; // expected-note{{implicit default constructor for 'NonLocalLambdaInstantation::X2<int *>' first required here}}
  118. }