2009-12-15 23:14:24 +03:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -verify %s
|
2009-10-08 02:35:40 +04:00
|
|
|
template<typename T, typename U = int> struct A; // expected-note {{template is declared here}} \
|
|
|
|
// expected-note{{explicitly specialized}}
|
2009-02-18 02:15:12 +03:00
|
|
|
|
2009-02-26 01:02:03 +03:00
|
|
|
template<> struct A<double, double>; // expected-note{{forward declaration}}
|
2009-02-18 02:15:12 +03:00
|
|
|
|
2009-02-26 01:02:03 +03:00
|
|
|
template<> struct A<float, float> { // expected-note{{previous definition}}
|
2009-02-18 02:15:12 +03:00
|
|
|
int x;
|
|
|
|
};
|
|
|
|
|
2009-02-26 01:02:03 +03:00
|
|
|
template<> struct A<float> { // expected-note{{previous definition}}
|
2009-02-18 02:15:12 +03:00
|
|
|
int y;
|
|
|
|
};
|
|
|
|
|
|
|
|
int test_specs(A<float, float> *a1, A<float, int> *a2) {
|
|
|
|
return a1->x + a2->y;
|
|
|
|
}
|
|
|
|
|
|
|
|
int test_incomplete_specs(A<double, double> *a1,
|
2009-03-03 07:44:36 +03:00
|
|
|
A<double> *a2)
|
2009-02-18 02:15:12 +03:00
|
|
|
{
|
2009-11-17 08:17:33 +03:00
|
|
|
(void)a1->x; // expected-error{{member access into incomplete type}}
|
2010-03-10 14:27:22 +03:00
|
|
|
(void)a2->x; // expected-error{{implicit instantiation of undefined template 'A<double, int>'}}
|
2009-02-18 02:15:12 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef float FLOAT;
|
|
|
|
|
2009-02-26 01:02:03 +03:00
|
|
|
template<> struct A<float, FLOAT>;
|
2009-02-18 02:15:12 +03:00
|
|
|
|
2009-02-26 01:02:03 +03:00
|
|
|
template<> struct A<FLOAT, float> { }; // expected-error{{redefinition}}
|
2009-02-18 02:15:12 +03:00
|
|
|
|
2009-02-26 01:02:03 +03:00
|
|
|
template<> struct A<float, int> { }; // expected-error{{redefinition}}
|
2009-02-19 03:52:42 +03:00
|
|
|
|
2009-02-26 01:02:03 +03:00
|
|
|
template<typename T, typename U = int> struct X;
|
2009-02-19 03:52:42 +03:00
|
|
|
|
2009-02-26 01:02:03 +03:00
|
|
|
template <> struct X<int, int> { int foo(); }; // #1
|
|
|
|
template <> struct X<float> { int bar(); }; // #2
|
2009-02-19 03:52:42 +03:00
|
|
|
|
|
|
|
typedef int int_type;
|
|
|
|
void testme(X<int_type> *x1, X<float, int> *x2) {
|
2009-02-26 03:02:51 +03:00
|
|
|
(void)x1->foo(); // okay: refers to #1
|
|
|
|
(void)x2->bar(); // okay: refers to #2
|
2009-02-19 03:52:42 +03:00
|
|
|
}
|
Implement parsing of nested-name-specifiers that involve template-ids, e.g.,
std::vector<int>::allocator_type
When we parse a template-id that names a type, it will become either a
template-id annotation (which is a parsed representation of a
template-id that has not yet been through semantic analysis) or a
typename annotation (where semantic analysis has resolved the
template-id to an actual type), depending on the context. We only
produce a type in contexts where we know that we only need type
information, e.g., in a type specifier. Otherwise, we create a
template-id annotation that can later be "upgraded" by transforming it
into a typename annotation when the parser needs a type. This occurs,
for example, when we've parsed "std::vector<int>" above and then see
the '::' after it. However, it means that when writing something like
this:
template<> class Outer::Inner<int> { ... };
We have two tokens to represent Outer::Inner<int>: one token for the
nested name specifier Outer::, and one template-id annotation token
for Inner<int>, which will be passed to semantic analysis to define
the class template specialization.
Most of the churn in the template tests in this patch come from an
improvement in our error recovery from ill-formed template-ids.
git-svn-id: https://llvm.org/svn/llvm-project/cfe/trunk@65467 91177308-0d34-0410-b5e6-96231b3b80d8
2009-02-25 22:37:18 +03:00
|
|
|
|
2009-02-26 03:02:51 +03:00
|
|
|
// Make sure specializations are proper classes.
|
|
|
|
template<>
|
|
|
|
struct A<char> {
|
|
|
|
A();
|
|
|
|
};
|
|
|
|
|
|
|
|
A<char>::A() { }
|
|
|
|
|
2009-08-26 22:54:58 +04:00
|
|
|
// Make sure we can see specializations defined before the primary template.
|
|
|
|
namespace N{
|
|
|
|
template<typename T> struct A0;
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace N {
|
|
|
|
template<>
|
|
|
|
struct A0<void> {
|
|
|
|
typedef void* pointer;
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace N {
|
|
|
|
template<typename T>
|
|
|
|
struct A0 {
|
|
|
|
void foo(A0<void>::pointer p = 0);
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
2009-02-26 03:02:51 +03:00
|
|
|
// Diagnose specialization errors
|
2009-10-27 09:26:26 +03:00
|
|
|
struct A<double> { }; // expected-error{{template specialization requires 'template<>'}}
|
2009-02-26 01:02:03 +03:00
|
|
|
|
|
|
|
template<> struct ::A<double>;
|
|
|
|
|
|
|
|
namespace N {
|
2009-10-08 02:35:40 +04:00
|
|
|
template<typename T> struct B; // expected-note 2{{explicitly specialized}}
|
2009-02-26 01:02:03 +03:00
|
|
|
|
2009-02-26 01:18:32 +03:00
|
|
|
template<> struct ::N::B<char>; // okay
|
2009-02-26 01:02:03 +03:00
|
|
|
template<> struct ::N::B<short>; // okay
|
|
|
|
template<> struct ::N::B<int>; // okay
|
2009-02-26 01:18:32 +03:00
|
|
|
|
|
|
|
int f(int);
|
2009-02-26 01:02:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
template<> struct N::B<int> { }; // okay
|
|
|
|
|
2011-12-30 01:57:33 +04:00
|
|
|
template<> struct N::B<float> { }; // expected-warning{{C++11 extension}}
|
2009-02-26 01:02:03 +03:00
|
|
|
|
|
|
|
namespace M {
|
|
|
|
template<> struct ::N::B<short> { }; // expected-error{{class template specialization of 'B' not in a namespace enclosing 'N'}}
|
|
|
|
|
2009-10-07 04:13:32 +04:00
|
|
|
template<> struct ::A<long double>; // expected-error{{originally}}
|
2009-02-26 01:02:03 +03:00
|
|
|
}
|
2009-02-26 01:18:32 +03:00
|
|
|
|
|
|
|
template<> struct N::B<char> {
|
|
|
|
int testf(int x) { return f(x); }
|
|
|
|
};
|
2009-02-26 03:02:51 +03:00
|
|
|
|
2009-10-27 09:26:26 +03:00
|
|
|
// PR5264
|
|
|
|
template <typename T> class Foo;
|
|
|
|
Foo<int>* v;
|
|
|
|
Foo<int>& F() { return *v; }
|
|
|
|
template <typename T> class Foo {};
|
|
|
|
Foo<int> x;
|
2009-11-12 03:46:20 +03:00
|
|
|
|
|
|
|
|
|
|
|
// Template template parameters
|
|
|
|
template<template<class T> class Wibble>
|
|
|
|
class Wibble<int> { }; // expected-error{{cannot specialize a template template parameter}}
|
2011-10-20 20:41:18 +04:00
|
|
|
|
|
|
|
namespace rdar9676205 {
|
|
|
|
template<typename T>
|
|
|
|
struct X {
|
|
|
|
template<typename U>
|
|
|
|
struct X<U*> { // expected-error{{explicit specialization of 'X' in class scope}}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|