constructor-template.cpp 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s
  2. struct X0 { // expected-note{{candidate}}
  3. X0(int); // expected-note{{candidate}}
  4. template<typename T> X0(T); // expected-note {{candidate}}
  5. template<typename T, typename U> X0(T*, U*); // expected-note {{candidate}}
  6. // PR4761
  7. template<typename T> X0() : f0(T::foo) {} // expected-note {{candidate}}
  8. int f0;
  9. };
  10. void accept_X0(X0);
  11. void test_X0(int i, float f) {
  12. X0 x0a(i);
  13. X0 x0b(f);
  14. X0 x0c = i;
  15. X0 x0d = f;
  16. accept_X0(i);
  17. accept_X0(&i);
  18. accept_X0(f);
  19. accept_X0(&f);
  20. X0 x0e(&i, &f);
  21. X0 x0f(&f, &i);
  22. X0 x0g(f, &i); // expected-error{{no matching constructor}}
  23. }
  24. template<typename T>
  25. struct X1 {
  26. X1(const X1&);
  27. template<typename U> X1(const X1<U>&);
  28. };
  29. template<typename T>
  30. struct Outer {
  31. typedef X1<T> A;
  32. A alloc;
  33. explicit Outer(const A& a) : alloc(a) { }
  34. };
  35. void test_X1(X1<int> xi) {
  36. Outer<int> oi(xi);
  37. Outer<float> of(xi);
  38. }
  39. // PR4655
  40. template<class C> struct A {};
  41. template <> struct A<int>{A(const A<int>&);};
  42. struct B { A<int> x; B(B& a) : x(a.x) {} };
  43. struct X2 {
  44. X2(); // expected-note{{candidate constructor}}
  45. X2(X2&); // expected-note {{candidate constructor}}
  46. template<typename T> X2(T); // expected-note {{candidate template ignored: instantiation would take its own class type by value}}
  47. };
  48. X2 test(bool Cond, X2 x2) {
  49. if (Cond)
  50. return x2; // okay, uses copy constructor
  51. return X2(); // expected-error{{no matching constructor}}
  52. }
  53. struct X3 {
  54. template<typename T> X3(T);
  55. };
  56. template<> X3::X3(X3); // expected-error{{must pass its first argument by reference}}
  57. struct X4 {
  58. X4();
  59. ~X4();
  60. X4(X4&);
  61. template<typename T> X4(const T&, int = 17);
  62. };
  63. X4 test_X4(bool Cond, X4 x4) {
  64. X4 a(x4, 17); // okay, constructor template
  65. X4 b(x4); // okay, copy constructor
  66. return X4();
  67. }
  68. // Instantiation of a non-dependent use of a constructor
  69. struct DefaultCtorHasDefaultArg {
  70. explicit DefaultCtorHasDefaultArg(int i = 17);
  71. };
  72. template<typename T>
  73. void default_ctor_inst() {
  74. DefaultCtorHasDefaultArg def;
  75. }
  76. template void default_ctor_inst<int>();
  77. template<typename T>
  78. struct X5 {
  79. X5();
  80. X5(const T &);
  81. };
  82. struct X6 {
  83. template<typename T> X6(T);
  84. };
  85. void test_X5_X6() {
  86. X5<X6> tf;
  87. X5<X6> tf2(tf);
  88. }
  89. namespace PR8182 {
  90. struct foo {
  91. foo();
  92. template<class T> foo(T&);
  93. private:
  94. foo(const foo&);
  95. };
  96. void test_foo() {
  97. foo f1;
  98. foo f2(f1);
  99. foo f3 = f1;
  100. }
  101. }
  102. // Don't blow out the stack trying to call an illegal constructor
  103. // instantiation. We intentionally allow implicit instantiations to
  104. // exist, so make sure they're unusable.
  105. //
  106. // rdar://19199836
  107. namespace self_by_value {
  108. template <class T, class U> struct A {
  109. A() {}
  110. A(const A<T,U> &o) {}
  111. A(A<T,T> o) {}
  112. };
  113. void helper(A<int,float>);
  114. void test1(A<int,int> a) {
  115. helper(a);
  116. }
  117. void test2() {
  118. helper(A<int,int>());
  119. }
  120. }
  121. namespace self_by_value_2 {
  122. template <class T, class U> struct A {
  123. A() {} // expected-note {{not viable: requires 0 arguments}}
  124. A(A<T,U> &o) {} // expected-note {{not viable: expects an l-value}}
  125. A(A<T,T> o) {} // expected-note {{ignored: instantiation takes its own class type by value}}
  126. };
  127. void helper_A(A<int,int>); // expected-note {{passing argument to parameter here}}
  128. void test_A() {
  129. helper_A(A<int,int>()); // expected-error {{no matching constructor}}
  130. }
  131. }
  132. namespace self_by_value_3 {
  133. template <class T, class U> struct A {
  134. A() {}
  135. A(A<T,U> &o) {}
  136. A(A<T,T> o) {}
  137. };
  138. void helper_A(A<int,int>);
  139. void test_A(A<int,int> b) {
  140. helper_A(b);
  141. }
  142. }