xref: /minix3/external/bsd/llvm/dist/clang/test/SemaTemplate/fun-template-def.cpp (revision f4a2713ac843a11c696ec80c0a5e3e5d80b4d338)
1*f4a2713aSLionel Sambuc // RUN: %clang_cc1 -fsyntax-only -verify %s
2*f4a2713aSLionel Sambuc 
3*f4a2713aSLionel Sambuc // Tests that dependent expressions are always allowed, whereas non-dependent
4*f4a2713aSLionel Sambuc // are checked as usual.
5*f4a2713aSLionel Sambuc 
6*f4a2713aSLionel Sambuc #include <stddef.h>
7*f4a2713aSLionel Sambuc 
8*f4a2713aSLionel Sambuc // Fake typeid, lacking a typeinfo header.
9*f4a2713aSLionel Sambuc namespace std { class type_info {}; }
10*f4a2713aSLionel Sambuc 
11*f4a2713aSLionel Sambuc struct dummy {}; // expected-note 3 {{candidate constructor (the implicit copy constructor)}}
12*f4a2713aSLionel Sambuc 
13*f4a2713aSLionel Sambuc template<typename T>
f0(T x)14*f4a2713aSLionel Sambuc int f0(T x) {
15*f4a2713aSLionel Sambuc   return (sizeof(x) == sizeof(int))? 0 : (sizeof(x) == sizeof(double))? 1 : 2;
16*f4a2713aSLionel Sambuc }
17*f4a2713aSLionel Sambuc 
18*f4a2713aSLionel Sambuc template <typename T, typename U>
f1(T t1,U u1,int i1)19*f4a2713aSLionel Sambuc T f1(T t1, U u1, int i1)
20*f4a2713aSLionel Sambuc {
21*f4a2713aSLionel Sambuc   T t2 = i1;
22*f4a2713aSLionel Sambuc   t2 = i1 + u1;
23*f4a2713aSLionel Sambuc   ++u1;
24*f4a2713aSLionel Sambuc   u1++;
25*f4a2713aSLionel Sambuc   int i2 = u1;
26*f4a2713aSLionel Sambuc 
27*f4a2713aSLionel Sambuc   i1 = t1[u1];
28*f4a2713aSLionel Sambuc   i1 *= t1;
29*f4a2713aSLionel Sambuc 
30*f4a2713aSLionel Sambuc   i1(u1, t1); // error
31*f4a2713aSLionel Sambuc   u1(i1, t1);
32*f4a2713aSLionel Sambuc 
33*f4a2713aSLionel Sambuc   U u2 = (T)i1;
34*f4a2713aSLionel Sambuc   static_cast<void>(static_cast<U>(reinterpret_cast<T>(
35*f4a2713aSLionel Sambuc     dynamic_cast<U>(const_cast<T>(i1)))));
36*f4a2713aSLionel Sambuc 
37*f4a2713aSLionel Sambuc   new U(i1, t1);
38*f4a2713aSLionel Sambuc   new int(t1, u1);
39*f4a2713aSLionel Sambuc   new (t1, u1) int;
40*f4a2713aSLionel Sambuc   delete t1;
41*f4a2713aSLionel Sambuc 
42*f4a2713aSLionel Sambuc   dummy d1 = sizeof(t1); // expected-error {{no viable conversion}}
43*f4a2713aSLionel Sambuc   dummy d2 = offsetof(T, foo); // expected-error {{no viable conversion}}
44*f4a2713aSLionel Sambuc   dummy d3 = __alignof(u1); // expected-error {{no viable conversion}}
45*f4a2713aSLionel Sambuc   i1 = typeid(t1); // expected-error {{assigning to 'int' from incompatible type 'const std::type_info'}}
46*f4a2713aSLionel Sambuc 
47*f4a2713aSLionel Sambuc   return u1;
48*f4a2713aSLionel Sambuc }
49*f4a2713aSLionel Sambuc 
50*f4a2713aSLionel Sambuc template<typename T>
f2(__restrict T x)51*f4a2713aSLionel Sambuc void f2(__restrict T x) {} // expected-note {{substitution failure [with T = int]: restrict requires a pointer or reference ('int' is invalid}}
52*f4a2713aSLionel Sambuc 
f3()53*f4a2713aSLionel Sambuc void f3() {
54*f4a2713aSLionel Sambuc   f2<int*>(0);
55*f4a2713aSLionel Sambuc   f2<int>(0); // expected-error {{no matching function for call to 'f2'}}
56*f4a2713aSLionel Sambuc }
57