2009-12-16 04:14:24 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -verify %s
|
2016-04-14 04:00:45 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
|
|
|
|
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
|
2009-10-08 06:35:40 +08:00
|
|
|
template<typename T, typename U = int> struct A; // expected-note {{template is declared here}} \
|
|
|
|
// expected-note{{explicitly specialized}}
|
2009-02-18 07:15:12 +08:00
|
|
|
|
2009-02-26 06:02:03 +08:00
|
|
|
template<> struct A<double, double>; // expected-note{{forward declaration}}
|
2009-02-18 07:15:12 +08:00
|
|
|
|
2009-02-26 06:02:03 +08:00
|
|
|
template<> struct A<float, float> { // expected-note{{previous definition}}
|
2009-02-18 07:15:12 +08:00
|
|
|
int x;
|
|
|
|
};
|
|
|
|
|
2009-02-26 06:02:03 +08:00
|
|
|
template<> struct A<float> { // expected-note{{previous definition}}
|
2009-02-18 07:15:12 +08: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 12:44:36 +08:00
|
|
|
A<double> *a2)
|
2009-02-18 07:15:12 +08:00
|
|
|
{
|
2009-11-17 13:17:33 +08:00
|
|
|
(void)a1->x; // expected-error{{member access into incomplete type}}
|
2010-03-10 19:27:22 +08:00
|
|
|
(void)a2->x; // expected-error{{implicit instantiation of undefined template 'A<double, int>'}}
|
2009-02-18 07:15:12 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
typedef float FLOAT;
|
|
|
|
|
2009-02-26 06:02:03 +08:00
|
|
|
template<> struct A<float, FLOAT>;
|
2009-02-18 07:15:12 +08:00
|
|
|
|
2009-02-26 06:02:03 +08:00
|
|
|
template<> struct A<FLOAT, float> { }; // expected-error{{redefinition}}
|
2009-02-18 07:15:12 +08:00
|
|
|
|
2009-02-26 06:02:03 +08:00
|
|
|
template<> struct A<float, int> { }; // expected-error{{redefinition}}
|
2009-02-19 08:52:42 +08:00
|
|
|
|
2009-02-26 06:02:03 +08:00
|
|
|
template<typename T, typename U = int> struct X;
|
2009-02-19 08:52:42 +08:00
|
|
|
|
2009-02-26 06:02:03 +08:00
|
|
|
template <> struct X<int, int> { int foo(); }; // #1
|
|
|
|
template <> struct X<float> { int bar(); }; // #2
|
2009-02-19 08:52:42 +08:00
|
|
|
|
|
|
|
typedef int int_type;
|
|
|
|
void testme(X<int_type> *x1, X<float, int> *x2) {
|
2009-02-26 08:02:51 +08:00
|
|
|
(void)x1->foo(); // okay: refers to #1
|
|
|
|
(void)x2->bar(); // okay: refers to #2
|
2009-02-19 08:52:42 +08: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.
llvm-svn: 65467
2009-02-26 03:37:18 +08:00
|
|
|
|
2009-02-26 08:02:51 +08:00
|
|
|
// Make sure specializations are proper classes.
|
|
|
|
template<>
|
|
|
|
struct A<char> {
|
|
|
|
A();
|
|
|
|
};
|
|
|
|
|
|
|
|
A<char>::A() { }
|
|
|
|
|
2009-08-27 02:54:58 +08: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 08:02:51 +08:00
|
|
|
// Diagnose specialization errors
|
2009-10-27 14:26:26 +08:00
|
|
|
struct A<double> { }; // expected-error{{template specialization requires 'template<>'}}
|
2009-02-26 06:02:03 +08:00
|
|
|
|
|
|
|
template<> struct ::A<double>;
|
|
|
|
|
|
|
|
namespace N {
|
2016-04-14 04:00:45 +08:00
|
|
|
template<typename T> struct B; // expected-note {{explicitly specialized}}
|
|
|
|
#if __cplusplus <= 199711L
|
|
|
|
// expected-note@-2 {{explicitly specialized}}
|
|
|
|
#endif
|
2009-02-26 06:02:03 +08:00
|
|
|
|
2009-02-26 06:18:32 +08:00
|
|
|
template<> struct ::N::B<char>; // okay
|
2009-02-26 06:02:03 +08:00
|
|
|
template<> struct ::N::B<short>; // okay
|
|
|
|
template<> struct ::N::B<int>; // okay
|
2009-02-26 06:18:32 +08:00
|
|
|
|
|
|
|
int f(int);
|
2009-02-26 06:02:03 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
template<> struct N::B<int> { }; // okay
|
|
|
|
|
2016-04-14 04:00:45 +08:00
|
|
|
template<> struct N::B<float> { };
|
|
|
|
#if __cplusplus <= 199711L
|
|
|
|
// expected-warning@-2 {{first declaration of class template specialization of 'B' outside namespace 'N' is a C++11 extension}}
|
|
|
|
#endif
|
|
|
|
|
2009-02-26 06:02:03 +08:00
|
|
|
|
|
|
|
namespace M {
|
|
|
|
template<> struct ::N::B<short> { }; // expected-error{{class template specialization of 'B' not in a namespace enclosing 'N'}}
|
|
|
|
|
2013-12-07 13:09:50 +08:00
|
|
|
template<> struct ::A<long double>; // expected-error{{must occur at global scope}}
|
2009-02-26 06:02:03 +08:00
|
|
|
}
|
2009-02-26 06:18:32 +08:00
|
|
|
|
|
|
|
template<> struct N::B<char> {
|
|
|
|
int testf(int x) { return f(x); }
|
|
|
|
};
|
2009-02-26 08:02:51 +08:00
|
|
|
|
2009-10-27 14:26:26 +08: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 08:46:20 +08:00
|
|
|
|
|
|
|
|
|
|
|
// Template template parameters
|
|
|
|
template<template<class T> class Wibble>
|
|
|
|
class Wibble<int> { }; // expected-error{{cannot specialize a template template parameter}}
|
2011-10-21 00:41:18 +08:00
|
|
|
|
|
|
|
namespace rdar9676205 {
|
|
|
|
template<typename T>
|
|
|
|
struct X {
|
|
|
|
template<typename U>
|
|
|
|
struct X<U*> { // expected-error{{explicit specialization of 'X' in class scope}}
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
2014-02-09 08:54:43 +08:00
|
|
|
|
|
|
|
namespace PR18009 {
|
|
|
|
template <typename T> struct A {
|
|
|
|
template <int N, int M> struct S;
|
|
|
|
template <int N> struct S<N, sizeof(T)> {};
|
|
|
|
};
|
|
|
|
A<int>::S<8, sizeof(int)> a; // ok
|
|
|
|
|
|
|
|
template <typename T> struct B {
|
2016-12-28 10:37:25 +08:00
|
|
|
template <int N, int M> struct S;
|
|
|
|
template <int N> struct S<N, sizeof(T) + N> {}; // ok (dr1315)
|
|
|
|
};
|
|
|
|
B<int>::S<8, sizeof(int) + 8> b;
|
|
|
|
|
|
|
|
template <typename T> struct C {
|
|
|
|
template <int N, int M> struct S;
|
|
|
|
template <int N> struct S<N, N ? **(T(*)[N])0 : 0> {}; // expected-error {{depends on a template parameter of the partial specialization}}
|
2014-02-09 08:54:43 +08:00
|
|
|
};
|
2016-12-28 10:37:25 +08:00
|
|
|
C<int> c; // expected-note {{in instantiation of}}
|
2014-02-09 08:54:43 +08:00
|
|
|
|
|
|
|
template<int A> struct outer {
|
|
|
|
template<int B, int C> struct inner {};
|
|
|
|
template<int C> struct inner<A * 2, C> {};
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
namespace PR16519 {
|
2016-04-14 04:00:45 +08:00
|
|
|
template<typename T, T...N> struct integer_sequence { typedef T value_type; };
|
|
|
|
#if __cplusplus <= 199711L
|
|
|
|
// expected-warning@-2 {{variadic templates are a C++11 extension}}
|
|
|
|
#endif
|
2014-02-09 08:54:43 +08:00
|
|
|
|
|
|
|
template<typename T> struct __make_integer_sequence;
|
2016-04-14 04:00:45 +08:00
|
|
|
template<typename T, T N> using make_integer_sequence = typename __make_integer_sequence<T>::template make<N, N % 2>::type;
|
|
|
|
#if __cplusplus <= 199711L
|
|
|
|
// expected-warning@-2 {{alias declarations are a C++11 extension}}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
template<typename T, typename T::value_type ...Extra> struct __make_integer_sequence_impl;
|
|
|
|
#if __cplusplus <= 199711L
|
|
|
|
// expected-warning@-2 {{variadic templates are a C++11 extension}}
|
|
|
|
#endif
|
|
|
|
|
2016-12-27 15:56:27 +08:00
|
|
|
// Note that the following seemingly-equivalent template parameter list is
|
|
|
|
// not OK; it would result in a partial specialization that is not more
|
|
|
|
// specialized than the primary template. (See NTTPTypeVsPartialOrder below.)
|
|
|
|
//
|
|
|
|
// template<typename T, T ...N, T ...Extra>
|
|
|
|
template<typename T, T ...N, typename integer_sequence<T, N...>::value_type ...Extra>
|
2016-04-14 04:00:45 +08:00
|
|
|
#if __cplusplus <= 199711L
|
2016-12-27 15:56:27 +08:00
|
|
|
// expected-warning@-2 2{{variadic templates are a C++11 extension}}
|
2016-04-14 04:00:45 +08:00
|
|
|
#endif
|
2016-12-27 15:56:27 +08:00
|
|
|
struct __make_integer_sequence_impl<integer_sequence<T, N...>, Extra...> {
|
2014-02-09 08:54:43 +08:00
|
|
|
typedef integer_sequence<T, N..., sizeof...(N) + N..., Extra...> type;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T> struct __make_integer_sequence {
|
|
|
|
template<T N, T Parity, typename = void> struct make;
|
|
|
|
template<typename Dummy> struct make<0, 0, Dummy> { typedef integer_sequence<T> type; };
|
|
|
|
template<typename Dummy> struct make<1, 1, Dummy> { typedef integer_sequence<T, 0> type; };
|
|
|
|
template<T N, typename Dummy> struct make<N, 0, Dummy> : __make_integer_sequence_impl<make_integer_sequence<T, N/2> > {};
|
|
|
|
template<T N, typename Dummy> struct make<N, 1, Dummy> : __make_integer_sequence_impl<make_integer_sequence<T, N/2>, N - 1> {};
|
|
|
|
};
|
|
|
|
|
2016-04-14 04:00:45 +08:00
|
|
|
using X = make_integer_sequence<int, 5>;
|
|
|
|
#if __cplusplus <= 199711L
|
|
|
|
// expected-warning@-2 {{alias declarations are a C++11 extension}}
|
|
|
|
#endif
|
|
|
|
|
|
|
|
using X = integer_sequence<int, 0, 1, 2, 3, 4>;
|
|
|
|
#if __cplusplus <= 199711L
|
|
|
|
// expected-warning@-2 {{alias declarations are a C++11 extension}}
|
|
|
|
#endif
|
2014-02-09 08:54:43 +08:00
|
|
|
}
|
|
|
|
|
2016-12-27 15:56:27 +08:00
|
|
|
namespace NTTPTypeVsPartialOrder {
|
|
|
|
struct X { typedef int value_type; };
|
|
|
|
template<typename T> struct Y { typedef T value_type; };
|
|
|
|
|
2017-01-17 10:14:37 +08:00
|
|
|
template<typename T, typename T::value_type N> struct A;
|
2016-12-27 15:56:27 +08:00
|
|
|
template<int N> struct A<X, N> {};
|
2017-01-17 10:14:37 +08:00
|
|
|
template<typename T, T N> struct A<Y<T>, N> {};
|
2016-12-27 15:56:27 +08:00
|
|
|
A<X, 0> ax;
|
|
|
|
A<Y<int>, 0> ay;
|
|
|
|
|
|
|
|
|
2017-01-17 10:14:37 +08:00
|
|
|
template<int, typename T, typename T::value_type> struct B;
|
|
|
|
template<typename T, typename T::value_type N> struct B<0, T, N>;
|
2016-12-27 15:56:27 +08:00
|
|
|
template<int N> struct B<0, X, N> {};
|
2017-01-17 10:14:37 +08:00
|
|
|
template<typename T, T N> struct B<0, Y<T>, N> {};
|
2016-12-27 15:56:27 +08:00
|
|
|
B<0, X, 0> bx;
|
2017-01-17 10:14:37 +08:00
|
|
|
B<0, Y<int>, 0> by;
|
2016-12-27 15:56:27 +08:00
|
|
|
}
|
|
|
|
|
2014-02-09 08:54:43 +08:00
|
|
|
namespace DefaultArgVsPartialSpec {
|
|
|
|
// Check that the diagnostic points at the partial specialization, not just at
|
|
|
|
// the default argument.
|
|
|
|
template<typename T, int N =
|
2016-12-28 10:37:25 +08:00
|
|
|
sizeof(T) // ok (dr1315)
|
2014-02-09 08:54:43 +08:00
|
|
|
> struct X {};
|
2016-12-28 10:37:25 +08:00
|
|
|
template<typename T> struct X<T> {};
|
2014-02-09 08:54:43 +08:00
|
|
|
|
|
|
|
template<typename T,
|
|
|
|
T N = 0 // expected-note {{template parameter is declared here}}
|
|
|
|
> struct S;
|
|
|
|
template<typename T> struct S<T> {}; // expected-error {{non-type template argument specializes a template parameter with dependent type 'T'}}
|
|
|
|
}
|