xref: /aosp_15_r20/external/clang/test/SemaTemplate/default-arguments-cxx0x.cpp (revision 67e74705e28f6214e480b399dd47ea732279e315)
1*67e74705SXin Li // RUN: %clang_cc1 -fsyntax-only -std=c++11 -verify %s
2*67e74705SXin Li // expected-no-diagnostics
3*67e74705SXin Li 
4*67e74705SXin Li // Test default template arguments for function templates.
5*67e74705SXin Li template<typename T = int>
6*67e74705SXin Li void f0();
7*67e74705SXin Li 
8*67e74705SXin Li template<typename T>
9*67e74705SXin Li void f0();
10*67e74705SXin Li 
g0()11*67e74705SXin Li void g0() {
12*67e74705SXin Li   f0(); // okay!
13*67e74705SXin Li }
14*67e74705SXin Li 
15*67e74705SXin Li template<typename T, int N = T::value>
16*67e74705SXin Li int &f1(T);
17*67e74705SXin Li 
18*67e74705SXin Li float &f1(...);
19*67e74705SXin Li 
20*67e74705SXin Li struct HasValue {
21*67e74705SXin Li   static const int value = 17;
22*67e74705SXin Li };
23*67e74705SXin Li 
g1()24*67e74705SXin Li void g1() {
25*67e74705SXin Li   float &fr = f1(15);
26*67e74705SXin Li   int &ir = f1(HasValue());
27*67e74705SXin Li }
28*67e74705SXin Li 
29*67e74705SXin Li namespace PR16689 {
30*67e74705SXin Li   template <typename T1, typename T2> class tuple {
31*67e74705SXin Li   public:
32*67e74705SXin Li       template <typename = T2>
tuple()33*67e74705SXin Li       constexpr tuple() {}
34*67e74705SXin Li   };
35*67e74705SXin Li   template <class X, class... Y> struct a : public X {
36*67e74705SXin Li     using X::X;
37*67e74705SXin Li   };
38*67e74705SXin Li   auto x = a<tuple<int, int> >();
39*67e74705SXin Li }
40*67e74705SXin Li 
41*67e74705SXin Li namespace PR16975 {
42*67e74705SXin Li   template <typename...> struct is {
operator boolPR16975::is43*67e74705SXin Li     constexpr operator bool() const { return false; }
44*67e74705SXin Li   };
45*67e74705SXin Li 
46*67e74705SXin Li   template <typename... Types>
47*67e74705SXin Li   struct bar {
48*67e74705SXin Li     template <typename T,
49*67e74705SXin Li               bool = is<Types...>()>
50*67e74705SXin Li     bar(T);
51*67e74705SXin Li   };
52*67e74705SXin Li 
53*67e74705SXin Li   struct baz : public bar<> {
54*67e74705SXin Li     using bar::bar;
55*67e74705SXin Li   };
56*67e74705SXin Li 
57*67e74705SXin Li   baz data{0};
58*67e74705SXin Li }
59*67e74705SXin Li 
60*67e74705SXin Li // rdar://23810407
61*67e74705SXin Li // An IRGen failure due to a symbol collision due to a default argument
62*67e74705SXin Li // being instantiated twice.  Credit goes to Richard Smith for this
63*67e74705SXin Li // reduction to a -fsyntax-only failure.
64*67e74705SXin Li namespace rdar23810407 {
65*67e74705SXin Li   // Instantiating the default argument multiple times will produce two
66*67e74705SXin Li   // different lambda types and thus instantiate this function multiple
67*67e74705SXin Li   // times, which will produce conflicting extern variable declarations.
f(T t)68*67e74705SXin Li   template<typename T> int f(T t) {
69*67e74705SXin Li     extern T rdar23810407_variable;
70*67e74705SXin Li     return 0;
71*67e74705SXin Li   }
__anon093939bc0102null72*67e74705SXin Li   template<typename T> int g(int a = f([] {}));
test()73*67e74705SXin Li   void test() {
74*67e74705SXin Li     g<int>();
75*67e74705SXin Li     g<int>();
76*67e74705SXin Li   }
77*67e74705SXin Li }
78*67e74705SXin Li 
79*67e74705SXin Li // rdar://problem/24480205
80*67e74705SXin Li namespace PR13986 {
81*67e74705SXin Li   constexpr unsigned Dynamic = 0;
82*67e74705SXin Li   template <unsigned> class A { template <unsigned = Dynamic> void m_fn1(); };
83*67e74705SXin Li   class Test {
~Test()84*67e74705SXin Li     ~Test() {}
85*67e74705SXin Li     A<1> m_target;
86*67e74705SXin Li   };
87*67e74705SXin Li }
88