constexpr-instantiate.cpp 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210
  1. // RUN: %clang_cc1 -std=c++11 -verify %s
  2. namespace UseBeforeDefinition {
  3. struct A {
  4. template<typename T> static constexpr T get() { return T(); }
  5. // ok, not a constant expression.
  6. int n = get<int>();
  7. };
  8. // ok, constant expression.
  9. constexpr int j = A::get<int>();
  10. template<typename T> constexpr int consume(T);
  11. // ok, not a constant expression.
  12. const int k = consume(0); // expected-note {{here}}
  13. template<typename T> constexpr int consume(T) { return 0; }
  14. // ok, constant expression.
  15. constexpr int l = consume(0);
  16. constexpr int m = k; // expected-error {{constant expression}} expected-note {{initializer of 'k'}}
  17. }
  18. namespace IntegralConst {
  19. template<typename T> constexpr T f(T n) { return n; }
  20. enum E {
  21. v = f(0), w = f(1) // ok
  22. };
  23. static_assert(w == 1, "");
  24. char arr[f('x')]; // ok
  25. static_assert(sizeof(arr) == 'x', "");
  26. }
  27. namespace ConvertedConst {
  28. template<typename T> constexpr T f(T n) { return n; }
  29. int f() {
  30. switch (f()) {
  31. case f(4): return 0;
  32. }
  33. return 1;
  34. }
  35. }
  36. namespace OverloadResolution {
  37. template<typename T> constexpr T f(T t) { return t; }
  38. template<int n> struct S { };
  39. template<typename T> auto g(T t) -> S<f(sizeof(T))> &;
  40. char &f(...);
  41. template<typename T> auto h(T t[f(sizeof(T))]) -> decltype(&*t) {
  42. return t;
  43. }
  44. S<4> &k = g(0);
  45. int *p, *q = h(p);
  46. }
  47. namespace DataMember {
  48. template<typename T> struct S { static const int k; };
  49. const int n = S<int>::k; // expected-note {{here}}
  50. template<typename T> const int S<T>::k = 0;
  51. constexpr int m = S<int>::k; // ok
  52. constexpr int o = n; // expected-error {{constant expression}} expected-note {{initializer of 'n'}}
  53. }
  54. namespace Reference {
  55. const int k = 5;
  56. template<typename T> struct S {
  57. static volatile int &r;
  58. };
  59. template<typename T> volatile int &S<T>::r = const_cast<volatile int&>(k);
  60. constexpr int n = const_cast<int&>(S<int>::r);
  61. static_assert(n == 5, "");
  62. }
  63. namespace Unevaluated {
  64. // We follow g++ in treating any reference to a constexpr function template
  65. // specialization as requiring an instantiation, even if it occurs in an
  66. // unevaluated context.
  67. //
  68. // We go slightly further than g++, and also trigger the implicit definition
  69. // of a defaulted special member in the same circumstances. This seems scary,
  70. // since a lot of classes have constexpr special members in C++11, but the
  71. // only observable impact should be the implicit instantiation of constexpr
  72. // special member templates (defaulted special members should only be
  73. // generated if they are well-formed, and non-constexpr special members in a
  74. // base or member cause the class's special member to not be constexpr).
  75. //
  76. // FIXME: None of this is required by the C++ standard. The rules in this
  77. // area are poorly specified, so this is subject to change.
  78. namespace NotConstexpr {
  79. template<typename T> struct S {
  80. S() : n(0) {}
  81. S(const S&) : n(T::error) {}
  82. int n;
  83. };
  84. struct U : S<int> {};
  85. decltype(U(U())) u; // ok, don't instantiate S<int>::S() because it wasn't declared constexpr
  86. }
  87. namespace Constexpr {
  88. template<typename T> struct S {
  89. constexpr S() : n(0) {}
  90. constexpr S(const S&) : n(T::error) {} // expected-error {{has no members}}
  91. int n;
  92. };
  93. struct U : S<int> {}; // expected-note {{instantiation}}
  94. decltype(U(U())) u; // expected-note {{here}}
  95. }
  96. namespace PR11851_Comment0 {
  97. template<int x> constexpr int f() { return x; }
  98. template<int i> void ovf(int (&x)[f<i>()]);
  99. void f() { int x[10]; ovf<10>(x); }
  100. }
  101. namespace PR11851_Comment1 {
  102. template<typename T>
  103. constexpr bool Integral() {
  104. return true;
  105. }
  106. template<typename T, bool Int = Integral<T>()>
  107. struct safe_make_unsigned {
  108. typedef T type;
  109. };
  110. template<typename T>
  111. using Make_unsigned = typename safe_make_unsigned<T>::type;
  112. template <typename T>
  113. struct get_distance_type {
  114. using type = int;
  115. };
  116. template<typename R>
  117. auto size(R) -> Make_unsigned<typename get_distance_type<R>::type>;
  118. auto check() -> decltype(size(0));
  119. }
  120. namespace PR11851_Comment6 {
  121. template<int> struct foo {};
  122. template<class> constexpr int bar() { return 0; }
  123. template<class T> foo<bar<T>()> foobar();
  124. auto foobar_ = foobar<int>();
  125. }
  126. namespace PR11851_Comment9 {
  127. struct S1 {
  128. constexpr S1() {}
  129. constexpr operator int() const { return 0; }
  130. };
  131. int k1 = sizeof(short{S1(S1())});
  132. struct S2 {
  133. constexpr S2() {}
  134. constexpr operator int() const { return 123456; }
  135. };
  136. int k2 = sizeof(short{S2(S2())}); // expected-error {{cannot be narrowed}} expected-note {{insert an explicit cast to silence this issue}}
  137. }
  138. namespace PR12288 {
  139. template <typename> constexpr bool foo() { return true; }
  140. template <bool> struct bar {};
  141. template <typename T> bar<foo<T>()> baz() { return bar<foo<T>()>(); }
  142. int main() { baz<int>(); }
  143. }
  144. namespace PR13423 {
  145. template<bool, typename> struct enable_if {};
  146. template<typename T> struct enable_if<true, T> { using type = T; };
  147. template<typename T> struct F {
  148. template<typename U>
  149. static constexpr bool f() { return sizeof(T) < U::size; }
  150. template<typename U>
  151. static typename enable_if<f<U>(), void>::type g() {} // expected-note {{disabled by 'enable_if'}}
  152. };
  153. struct U { static constexpr int size = 2; };
  154. void h() { F<char>::g<U>(); }
  155. void i() { F<int>::g<U>(); } // expected-error {{no matching function}}
  156. }
  157. namespace PR14203 {
  158. struct duration { constexpr duration() {} };
  159. template <typename>
  160. void sleep_for() {
  161. constexpr duration max = duration();
  162. }
  163. }
  164. }
  165. namespace NoInstantiationWhenSelectingOverload {
  166. // Check that we don't instantiate conversion functions when we're checking
  167. // for the existence of an implicit conversion sequence, only when a function
  168. // is actually chosen by overload resolution.
  169. struct S {
  170. template<typename T> constexpr S(T) : n(T::error) {} // expected-error {{no members}}
  171. int n;
  172. };
  173. int f(S);
  174. int f(int);
  175. void g() { f(0); }
  176. void h() { (void)sizeof(f(0)); }
  177. void i() { (void)sizeof(f("oops")); } // expected-note {{instantiation of}}
  178. }