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