blob: f9015b37ea0932d4bba41e8b8755dae6ac46b891 [file] [log] [blame]
Daniel Dunbara5728872009-12-15 20:14:24 +00001// RUN: %clang_cc1 -fsyntax-only -verify %s
Douglas Gregor1fef4e62009-10-07 22:35:40 +00002template<typename T, typename U = int> struct A; // expected-note {{template is declared here}} \
3 // expected-note{{explicitly specialized}}
Douglas Gregorcc636682009-02-17 23:15:12 +00004
Douglas Gregor88b70942009-02-25 22:02:03 +00005template<> struct A<double, double>; // expected-note{{forward declaration}}
Douglas Gregorcc636682009-02-17 23:15:12 +00006
Douglas Gregor88b70942009-02-25 22:02:03 +00007template<> struct A<float, float> { // expected-note{{previous definition}}
Douglas Gregorcc636682009-02-17 23:15:12 +00008 int x;
9};
10
Douglas Gregor88b70942009-02-25 22:02:03 +000011template<> struct A<float> { // expected-note{{previous definition}}
Douglas Gregorcc636682009-02-17 23:15:12 +000012 int y;
13};
14
15int test_specs(A<float, float> *a1, A<float, int> *a2) {
16 return a1->x + a2->y;
17}
18
19int test_incomplete_specs(A<double, double> *a1,
Douglas Gregor2943aed2009-03-03 04:44:36 +000020 A<double> *a2)
Douglas Gregorcc636682009-02-17 23:15:12 +000021{
Douglas Gregor03c57052009-11-17 05:17:33 +000022 (void)a1->x; // expected-error{{member access into incomplete type}}
John McCall7c2342d2010-03-10 11:27:22 +000023 (void)a2->x; // expected-error{{implicit instantiation of undefined template 'A<double, int>'}}
Douglas Gregorcc636682009-02-17 23:15:12 +000024}
25
26typedef float FLOAT;
27
Douglas Gregor88b70942009-02-25 22:02:03 +000028template<> struct A<float, FLOAT>;
Douglas Gregorcc636682009-02-17 23:15:12 +000029
Douglas Gregor88b70942009-02-25 22:02:03 +000030template<> struct A<FLOAT, float> { }; // expected-error{{redefinition}}
Douglas Gregorcc636682009-02-17 23:15:12 +000031
Douglas Gregor88b70942009-02-25 22:02:03 +000032template<> struct A<float, int> { }; // expected-error{{redefinition}}
Douglas Gregor611a8c42009-02-19 00:52:42 +000033
Douglas Gregor88b70942009-02-25 22:02:03 +000034template<typename T, typename U = int> struct X;
Douglas Gregor611a8c42009-02-19 00:52:42 +000035
Douglas Gregor88b70942009-02-25 22:02:03 +000036template <> struct X<int, int> { int foo(); }; // #1
37template <> struct X<float> { int bar(); }; // #2
Douglas Gregor611a8c42009-02-19 00:52:42 +000038
39typedef int int_type;
40void testme(X<int_type> *x1, X<float, int> *x2) {
Douglas Gregor65100792009-02-26 00:02:51 +000041 (void)x1->foo(); // okay: refers to #1
42 (void)x2->bar(); // okay: refers to #2
Douglas Gregor611a8c42009-02-19 00:52:42 +000043}
Douglas Gregor39a8de12009-02-25 19:37:18 +000044
Douglas Gregor65100792009-02-26 00:02:51 +000045// Make sure specializations are proper classes.
46template<>
47struct A<char> {
48 A();
49};
50
51A<char>::A() { }
52
Douglas Gregordb3a0f52009-08-26 18:54:58 +000053// Make sure we can see specializations defined before the primary template.
54namespace N{
55 template<typename T> struct A0;
56}
57
58namespace N {
59 template<>
60 struct A0<void> {
61 typedef void* pointer;
62 };
63}
64
65namespace N {
66 template<typename T>
67 struct A0 {
68 void foo(A0<void>::pointer p = 0);
69 };
70}
71
Douglas Gregor65100792009-02-26 00:02:51 +000072// Diagnose specialization errors
Douglas Gregor972e6ce2009-10-27 06:26:26 +000073struct A<double> { }; // expected-error{{template specialization requires 'template<>'}}
Douglas Gregor88b70942009-02-25 22:02:03 +000074
Douglas Gregor88b70942009-02-25 22:02:03 +000075template<> struct ::A<double>;
76
77namespace N {
Douglas Gregor1fef4e62009-10-07 22:35:40 +000078 template<typename T> struct B; // expected-note 2{{explicitly specialized}}
Douglas Gregor88b70942009-02-25 22:02:03 +000079
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +000080 template<> struct ::N::B<char>; // okay
Douglas Gregor88b70942009-02-25 22:02:03 +000081 template<> struct ::N::B<short>; // okay
82 template<> struct ::N::B<int>; // okay
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +000083
84 int f(int);
Douglas Gregor88b70942009-02-25 22:02:03 +000085}
86
87template<> struct N::B<int> { }; // okay
88
Richard Smithd7c56e12011-12-29 21:57:33 +000089template<> struct N::B<float> { }; // expected-warning{{C++11 extension}}
Douglas Gregor88b70942009-02-25 22:02:03 +000090
91namespace M {
92 template<> struct ::N::B<short> { }; // expected-error{{class template specialization of 'B' not in a namespace enclosing 'N'}}
93
Douglas Gregord5cb8762009-10-07 00:13:32 +000094 template<> struct ::A<long double>; // expected-error{{originally}}
Douglas Gregor88b70942009-02-25 22:02:03 +000095}
Douglas Gregor6bc9f7e2009-02-25 22:18:32 +000096
97template<> struct N::B<char> {
98 int testf(int x) { return f(x); }
99};
Douglas Gregor65100792009-02-26 00:02:51 +0000100
Douglas Gregor972e6ce2009-10-27 06:26:26 +0000101// PR5264
102template <typename T> class Foo;
103Foo<int>* v;
104Foo<int>& F() { return *v; }
105template <typename T> class Foo {};
106Foo<int> x;
Douglas Gregor8b13c082009-11-12 00:46:20 +0000107
108
109// Template template parameters
110template<template<class T> class Wibble>
111class Wibble<int> { }; // expected-error{{cannot specialize a template template parameter}}
Douglas Gregor8e0c1182011-10-20 16:41:18 +0000112
113namespace rdar9676205 {
114 template<typename T>
115 struct X {
116 template<typename U>
117 struct X<U*> { // expected-error{{explicit specialization of 'X' in class scope}}
118 };
119 };
120
121}