instantiate-member-class.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s
  2. namespace PR8965 {
  3. template<typename T>
  4. struct X {
  5. typedef int type;
  6. T field; // expected-note{{in instantiation of member class}}
  7. };
  8. template<typename T>
  9. struct Y {
  10. struct Inner;
  11. typedef typename X<Inner>::type // expected-note{{in instantiation of template class}}
  12. type; // expected-note{{not-yet-instantiated member is declared here}}
  13. struct Inner {
  14. typedef type field; // expected-error{{no member 'type' in 'PR8965::Y<int>'; it has not yet been instantiated}}
  15. };
  16. };
  17. Y<int> y; // expected-note{{in instantiation of template class}}
  18. }
  19. template<typename T>
  20. class X {
  21. public:
  22. struct C { T &foo(); };
  23. struct D {
  24. struct E { T &bar(); }; // expected-error{{cannot form a reference to 'void'}}
  25. struct F; // expected-note{{member is declared here}}
  26. };
  27. };
  28. X<int>::C *c1;
  29. X<float>::C *c2;
  30. X<int>::X *xi; // expected-error{{qualified reference to 'X' is a constructor name rather than a type wherever a constructor can be declared}}
  31. X<float>::X *xf; // expected-error{{qualified reference to 'X' is a constructor name rather than a type wherever a constructor can be declared}}
  32. void test_naming() {
  33. c1 = c2; // expected-error{{assigning to 'X<int>::C *' from incompatible type 'X<float>::C *'}}
  34. xi = xf; // expected-error{{assigning to 'X<int>::X<int> *' from incompatible type 'X<float>::X<float> *'}}
  35. // FIXME: error above doesn't print the type X<int>::X cleanly!
  36. }
  37. void test_instantiation(X<double>::C *x,
  38. X<float>::D::E *e,
  39. X<float>::D::F *f) {
  40. double &dr = x->foo();
  41. float &fr = e->bar();
  42. f->foo(); // expected-error{{implicit instantiation of undefined member 'X<float>::D::F'}}
  43. }
  44. X<void>::C *c3; // okay
  45. X<void>::D::E *e1; // okay
  46. X<void>::D::E e2; // expected-note{{in instantiation of member class 'X<void>::D::E' requested here}}
  47. // Redeclarations.
  48. namespace test1 {
  49. template <typename T> struct Registry {
  50. struct node;
  51. static node *Head;
  52. struct node {
  53. node(int v) { Head = this; }
  54. };
  55. };
  56. void test() {
  57. Registry<int>::node node(0);
  58. }
  59. }
  60. // Redeclarations during explicit instantiations.
  61. namespace test2 {
  62. template <typename T> class A {
  63. class Foo;
  64. class Foo {
  65. int foo();
  66. };
  67. };
  68. template class A<int>;
  69. template <typename T> class B {
  70. class Foo;
  71. class Foo {
  72. public:
  73. typedef int X;
  74. };
  75. typename Foo::X x;
  76. };
  77. template class B<int>;
  78. template <typename T> class C {
  79. class Foo;
  80. };
  81. template <typename T> class C<T>::Foo {
  82. int x;
  83. };
  84. template class C<int>;
  85. }
  86. namespace AliasTagDef {
  87. template<typename T>
  88. struct F {
  89. using S = struct U { // expected-warning {{C++11}}
  90. T g() {
  91. return T();
  92. }
  93. };
  94. };
  95. int m = F<int>::S().g();
  96. int n = F<int>::U().g();
  97. }
  98. namespace rdar10397846 {
  99. template<int I> struct A
  100. {
  101. struct B
  102. {
  103. struct C { C() { int *ptr = I; } }; // expected-error{{cannot initialize a variable of type 'int *' with an rvalue of type 'int'}} \
  104. expected-warning{{expression which evaluates to zero treated as a null pointer constant of type 'int *'}}
  105. };
  106. };
  107. template<int N> void foo()
  108. {
  109. class A<N>::B::C X; // expected-note 2 {{in instantiation of member function}}
  110. int A<N+1>::B::C::*member = 0;
  111. }
  112. void bar()
  113. {
  114. foo<0>(); // expected-note{{in instantiation of function template}}
  115. foo<1>(); // expected-note{{in instantiation of function template}}
  116. }
  117. }