2009-12-16 04:14:24 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -verify %s
|
2015-11-12 03:34:47 +08:00
|
|
|
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++98 %s
|
|
|
|
// RUN: %clang_cc1 -fsyntax-only -verify -std=c++11 %s
|
2009-02-12 02:16:40 +08:00
|
|
|
template<typename T, int N = 2> struct X; // expected-note{{template is declared here}}
|
|
|
|
|
|
|
|
X<int, 1> *x1;
|
|
|
|
X<int> *x2;
|
|
|
|
|
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
|
|
|
X<> *x3; // expected-error{{too few template arguments for class template 'X'}}
|
2009-02-12 02:16:40 +08:00
|
|
|
|
|
|
|
template<typename U = float, int M> struct X;
|
|
|
|
|
|
|
|
X<> *x4;
|
2009-06-05 10:12:32 +08:00
|
|
|
|
2009-06-05 10:45:24 +08:00
|
|
|
template<typename T = int> struct Z { };
|
2009-06-05 10:12:32 +08:00
|
|
|
template struct Z<>;
|
2009-06-12 00:06:49 +08:00
|
|
|
|
|
|
|
// PR4362
|
|
|
|
template<class T> struct a { };
|
|
|
|
template<> struct a<int> { static const bool v = true; };
|
|
|
|
|
|
|
|
template<class T, bool = a<T>::v> struct p { }; // expected-error {{no member named 'v'}}
|
|
|
|
|
|
|
|
template struct p<bool>; // expected-note {{in instantiation of default argument for 'p<bool>' required here}}
|
|
|
|
template struct p<int>;
|
2009-10-15 01:30:58 +08:00
|
|
|
|
|
|
|
// PR5187
|
|
|
|
template<typename T, typename U>
|
|
|
|
struct A;
|
|
|
|
|
|
|
|
template<typename T, typename U = T>
|
|
|
|
struct A;
|
|
|
|
|
|
|
|
template<typename T, typename U>
|
|
|
|
struct A {
|
|
|
|
void f(A<T>);
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct B { };
|
|
|
|
|
|
|
|
template<>
|
|
|
|
struct B<void> {
|
|
|
|
typedef B<void*> type;
|
|
|
|
};
|
2009-11-10 03:17:50 +08:00
|
|
|
|
|
|
|
// Nested default arguments for template parameters.
|
|
|
|
template<typename T> struct X1 { };
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct X2 {
|
2013-08-29 07:48:32 +08:00
|
|
|
template<typename U = typename X1<T>::type> // expected-error{{no type named 'type' in 'X1<int>'}} \
|
|
|
|
// expected-error{{no type named 'type' in 'X1<char>'}}
|
|
|
|
struct Inner1 { }; // expected-note{{template is declared here}}
|
2009-11-10 03:17:50 +08:00
|
|
|
|
2013-08-29 07:48:32 +08:00
|
|
|
template<T Value = X1<T>::value> // expected-error{{no member named 'value' in 'X1<int>'}} \
|
|
|
|
// expected-error{{no member named 'value' in 'X1<char>'}}
|
|
|
|
struct NonType1 { }; // expected-note{{template is declared here}}
|
2009-11-10 03:17:50 +08:00
|
|
|
|
|
|
|
template<T Value>
|
|
|
|
struct Inner2 { };
|
|
|
|
|
|
|
|
template<typename U>
|
|
|
|
struct Inner3 {
|
|
|
|
template<typename X = T, typename V = U>
|
|
|
|
struct VeryInner { };
|
|
|
|
|
|
|
|
template<T Value1 = sizeof(T), T Value2 = sizeof(U),
|
|
|
|
T Value3 = Value1 + Value2>
|
|
|
|
struct NonType2 { };
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
2013-08-29 07:48:32 +08:00
|
|
|
X2<int> x2i; // expected-note{{in instantiation of template class 'X2<int>' requested here}}
|
2009-11-10 03:17:50 +08:00
|
|
|
X2<int>::Inner1<float> x2iif;
|
|
|
|
|
2013-08-29 07:48:32 +08:00
|
|
|
X2<int>::Inner1<> x2bad; // expected-error{{too few template arguments for class template 'Inner1'}}
|
2009-11-10 03:17:50 +08:00
|
|
|
|
|
|
|
X2<int>::NonType1<'a'> x2_nontype1;
|
2013-08-29 07:48:32 +08:00
|
|
|
X2<int>::NonType1<> x2_nontype1_bad; // expected-error{{too few template arguments for class template 'NonType1'}}
|
2009-11-10 03:17:50 +08:00
|
|
|
|
|
|
|
// Check multi-level substitution into template type arguments
|
|
|
|
X2<int>::Inner3<float>::VeryInner<> vi;
|
2013-08-29 07:48:32 +08:00
|
|
|
X2<char>::Inner3<int>::NonType2<> x2_deep_nontype; // expected-note{{in instantiation of template class 'X2<char>' requested here}}
|
2009-11-10 03:17:50 +08:00
|
|
|
|
|
|
|
template<typename T, typename U>
|
|
|
|
struct is_same { static const bool value = false; };
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
struct is_same<T, T> { static const bool value = true; };
|
|
|
|
|
2009-11-12 00:39:34 +08:00
|
|
|
int array1[is_same<__typeof__(vi),
|
2009-11-10 03:17:50 +08:00
|
|
|
X2<int>::Inner3<float>::VeryInner<int, float> >::value? 1 : -1];
|
|
|
|
|
2009-11-12 00:39:34 +08:00
|
|
|
int array2[is_same<__typeof(x2_deep_nontype),
|
|
|
|
X2<char>::Inner3<int>::NonType2<sizeof(char), sizeof(int),
|
2009-11-10 03:17:50 +08:00
|
|
|
sizeof(char)+sizeof(int)> >::value? 1 : -1];
|
2009-11-12 00:39:34 +08:00
|
|
|
|
|
|
|
// Template template parameter defaults
|
|
|
|
template<template<typename T> class X = X2> struct X3 { };
|
|
|
|
int array3[is_same<X3<>, X3<X2> >::value? 1 : -1];
|
|
|
|
|
|
|
|
struct add_pointer {
|
|
|
|
template<typename T>
|
|
|
|
struct apply {
|
|
|
|
typedef T* type;
|
|
|
|
};
|
|
|
|
};
|
|
|
|
|
|
|
|
template<typename T, template<typename> class X = T::template apply>
|
|
|
|
struct X4;
|
|
|
|
int array4[is_same<X4<add_pointer>,
|
|
|
|
X4<add_pointer, add_pointer::apply> >::value? 1 : -1];
|
2009-11-12 03:13:48 +08:00
|
|
|
|
|
|
|
template<int> struct X5 {}; // expected-note{{has a different type 'int'}}
|
|
|
|
template<long> struct X5b {};
|
|
|
|
template<typename T,
|
|
|
|
template<T> class B = X5> // expected-error{{template template argument has different}} \
|
|
|
|
// expected-note{{previous non-type template parameter}}
|
|
|
|
struct X6 {};
|
|
|
|
|
|
|
|
X6<int> x6a;
|
2009-11-12 05:54:23 +08:00
|
|
|
X6<long> x6b; // expected-note{{while checking a default template argument}}
|
2009-11-12 03:13:48 +08:00
|
|
|
X6<long, X5b> x6c;
|
2009-11-12 08:03:40 +08:00
|
|
|
|
|
|
|
|
|
|
|
template<template<class> class X = B<int> > struct X7; // expected-error{{must be a class template}}
|
2011-06-15 22:20:42 +08:00
|
|
|
|
|
|
|
namespace PR9643 {
|
|
|
|
template<typename T> class allocator {};
|
|
|
|
template<typename T, typename U = allocator<T> > class vector {};
|
|
|
|
|
|
|
|
template<template<typename U, typename = allocator<U> > class container,
|
|
|
|
typename DT>
|
|
|
|
container<DT> initializer(const DT& d) {
|
|
|
|
return container<DT>();
|
|
|
|
}
|
|
|
|
|
|
|
|
void f() {
|
|
|
|
vector<int, allocator<int> > v = initializer<vector>(5);
|
|
|
|
}
|
|
|
|
}
|
2013-07-22 11:31:14 +08:00
|
|
|
|
|
|
|
namespace PR16288 {
|
|
|
|
template<typename X>
|
|
|
|
struct S {
|
2015-11-12 03:34:47 +08:00
|
|
|
template<typename T = int, typename U>
|
|
|
|
#if __cplusplus <= 199711L // C++03 or earlier modes
|
|
|
|
// expected-warning@-2 {{default template arguments for a function template are a C++11 extension}}
|
|
|
|
#endif
|
2013-07-22 11:31:14 +08:00
|
|
|
void f();
|
|
|
|
};
|
|
|
|
template<typename X>
|
|
|
|
template<typename T, typename U>
|
|
|
|
void S<X>::f() {}
|
|
|
|
}
|
2013-08-29 07:48:32 +08:00
|
|
|
|
|
|
|
namespace DR1635 {
|
|
|
|
template <class T> struct X {
|
2015-11-12 03:34:47 +08:00
|
|
|
template <class U = typename T::type> static void f(int) {} // expected-error {{type 'int' cannot be used prior to '::' because it has no members}}
|
|
|
|
#if __cplusplus <= 199711L // C++03 or earlier modes
|
|
|
|
// expected-warning@-2 {{default template arguments for a function template are a C++11 extension}}
|
|
|
|
#endif
|
2013-08-29 07:48:32 +08:00
|
|
|
static void f(...) {}
|
|
|
|
};
|
|
|
|
|
|
|
|
int g() { X<int>::f(0); } // expected-note {{in instantiation of template class 'DR1635::X<int>' requested here}}
|
|
|
|
}
|
2015-08-23 18:22:28 +08:00
|
|
|
|
|
|
|
namespace NondefDecls {
|
|
|
|
template<typename T> void f1() {
|
|
|
|
int g1(int defarg = T::error); // expected-error{{type 'int' cannot be used prior to '::' because it has no members}}
|
|
|
|
}
|
|
|
|
template void f1<int>(); // expected-note{{in instantiation of function template specialization 'NondefDecls::f1<int>' requested here}}
|
|
|
|
}
|
2015-09-02 06:50:31 +08:00
|
|
|
|
|
|
|
template <typename T>
|
|
|
|
struct C {
|
|
|
|
C(T t = ); // expected-error {{expected expression}}
|
|
|
|
};
|
|
|
|
C<int> obj;
|
Fix PR26134: When substituting into default template arguments, keep CurContext unchanged.
Or, do not set Sema's CurContext to the template declaration's when substituting into default template arguments of said template declaration.
If we do push the template declaration context on to Sema, and the template declaration is at namespace scope, Sema can get confused and try and do odr analysis when substituting into default template arguments, even though the substitution could be occurring within a dependent context.
I'm not sure why this was being done, perhaps there was concern that if a default template argument referred to a previous template parameter, it might not be found during substitution - but all regression tests pass, and I can't craft a test that would cause it to fails (if some one does, please inform me, and i'll craft a different fix for the PR).
This patch removes a single line of code, but unfortunately adds more than it removes, because of the tests. Some day I still hope to commit a patch that removes far more lines than it adds, while leaving clang better for it ;)
Sorry that r253590 ("Change the expression evaluation context from Unevaluated to ConstantEvaluated while substituting into non-type template argument defaults") caused the PR!
llvm-svn: 258110
2016-01-19 11:58:55 +08:00
|
|
|
|
|
|
|
namespace PR26134 {
|
|
|
|
// Make sure when substituting default template arguments we do it in the current context.
|
|
|
|
template<class T, bool Val = T::value>
|
|
|
|
struct X {};
|
|
|
|
|
|
|
|
template<bool B> struct Y {
|
|
|
|
void f() { X<Y> xy; }
|
|
|
|
static const bool value = B;
|
|
|
|
};
|
|
|
|
|
|
|
|
namespace ns1 {
|
|
|
|
template<class T0>
|
|
|
|
struct X {
|
|
|
|
template<bool B = T0::value> struct XInner { static const bool value = B; };
|
|
|
|
};
|
|
|
|
template<bool B> struct S { static const bool value = B; };
|
|
|
|
#if __cplusplus > 199711L
|
|
|
|
template<bool B> struct Y {
|
|
|
|
static constexpr bool f() { return typename X<S<B>>::template XInner<>{}.value; }
|
|
|
|
static_assert(f() == B, "");
|
|
|
|
};
|
|
|
|
Y<true> y;
|
|
|
|
Y<false> y2;
|
|
|
|
#endif
|
|
|
|
|
|
|
|
} // end ns1
|
|
|
|
} // end ns PR26134
|
2017-05-10 08:01:13 +08:00
|
|
|
|
|
|
|
namespace friends {
|
|
|
|
namespace ns {
|
|
|
|
template<typename> struct A {
|
|
|
|
template<typename> friend void f();
|
|
|
|
template<typename> friend struct X;
|
|
|
|
};
|
|
|
|
template<typename = int> void f(); // expected-warning 0-1{{extension}}
|
|
|
|
template<typename = int> struct X;
|
|
|
|
A<int> a;
|
|
|
|
}
|
|
|
|
namespace ns {
|
|
|
|
void g() { f(); }
|
|
|
|
X<int> *p;
|
|
|
|
}
|
|
|
|
}
|
2019-06-12 07:51:46 +08:00
|
|
|
|
|
|
|
namespace unevaluated {
|
|
|
|
int a;
|
|
|
|
template<int = 0> int f(int = a); // expected-warning 0-1{{extension}}
|
|
|
|
int k = sizeof(f());
|
|
|
|
}
|