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