function-template-specialization.cpp 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. // RUN: %clang_cc1 -fsyntax-only -verify %s
  2. template <int N>
  3. void f0(int (&array)[N]); // expected-note {{candidate template ignored: could not match 'int' against 'char'}}
  4. // Simple function template specialization (using overloading)
  5. template<> void f0(int (&array)[1]);
  6. void test_f0() {
  7. int iarr1[1];
  8. f0(iarr1);
  9. }
  10. // Function template specialization where there are no matches
  11. template<> void f0(char (&array)[1]); // expected-error{{no function template matches}}
  12. template<> void f0<2>(int (&array)[2]) { }
  13. // Function template specialization that requires partial ordering
  14. template<typename T, int N> void f1(T (&array)[N]); // expected-note{{matches}}
  15. template<int N> void f1(int (&array)[N]); // expected-note{{matches}}
  16. template<> void f1(float (&array)[1]);
  17. template<> void f1(int (&array)[1]);
  18. // Function template specialization that results in an ambiguity
  19. template<typename T> void f1(T (&array)[17]); // expected-note{{matches}}
  20. template<> void f1(int (&array)[17]); // expected-error{{ambiguous}}
  21. // Resolving that ambiguity with explicitly-specified template arguments.
  22. template<int N> void f2(double (&array)[N]);
  23. template<typename T> void f2(T (&array)[42]);
  24. template<> void f2<double>(double (&array)[42]);
  25. template<> void f2<42>(double (&array)[42]);
  26. void f2<25>(double (&array)[25]); // expected-error{{specialization}}
  27. // PR5833
  28. namespace PR5833 {
  29. template <typename T> bool f0(T &t1);
  30. template <> bool f0<float>(float &t1);
  31. }
  32. template <> bool PR5833::f0<float>(float &t1) {}
  33. // PR8295
  34. namespace PR8295 {
  35. template <typename T> void f(T t) {}
  36. template <typename T> void f<T*>(T* t) {} // expected-error{{function template partial specialization is not allowed}}
  37. }
  38. class Foo {
  39. template<class T>
  40. static void Bar(const T& input);
  41. // Don't crash here.
  42. template<>
  43. static void Bar(const long& input) {} // expected-error{{explicit specialization of 'Bar' in class scope}}
  44. };