fun-template-def.cpp revision 93d6b07cd79d74e343d81c0e8fb5365376a33097
1a5728872c7702ddd09537c95bc3cbd20e1f2fb09Daniel Dunbar// RUN: %clang_cc1 -fsyntax-only -verify %s
22850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl
32850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl// Tests that dependent expressions are always allowed, whereas non-dependent
42850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl// are checked as usual.
52850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl
62850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl#include <stddef.h>
72850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl
82850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl// Fake typeid, lacking a typeinfo header.
92850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redlnamespace std { class type_info {}; }
102850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl
11220ccbf2c9ef97034cce80561f9f46c4f1f63bc7John McCallstruct dummy {}; // expected-note 3 {{candidate constructor (the implicit copy constructor)}}
122850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl
139983cc110422a2e976191a0ce6ac4f1d7b634cc0Douglas Gregortemplate<typename T>
149983cc110422a2e976191a0ce6ac4f1d7b634cc0Douglas Gregorint f0(T x) {
159983cc110422a2e976191a0ce6ac4f1d7b634cc0Douglas Gregor  return (sizeof(x) == sizeof(int))? 0 : (sizeof(x) == sizeof(double))? 1 : 2;
169983cc110422a2e976191a0ce6ac4f1d7b634cc0Douglas Gregor}
179983cc110422a2e976191a0ce6ac4f1d7b634cc0Douglas Gregor
182850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redltemplate <typename T, typename U>
199983cc110422a2e976191a0ce6ac4f1d7b634cc0Douglas GregorT f1(T t1, U u1, int i1)
202850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl{
212850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl  T t2 = i1;
222850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl  t2 = i1 + u1;
232850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl  ++u1;
242850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl  u1++;
252850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl  int i2 = u1;
262850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl
272850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl  i1 = t1[u1];
282850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl  i1 *= t1;
292850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl
302850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl  i1(u1, t1); // error
312850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl  u1(i1, t1);
322850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl
332850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl  U u2 = (T)i1;
342850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl  static_cast<void>(static_cast<U>(reinterpret_cast<T>(
352850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl    dynamic_cast<U>(const_cast<T>(i1)))));
362850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl
372850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl  new U(i1, t1);
38089407be3fb616fb1246f2aee29b8a9c58ec7807Douglas Gregor  new int(t1, u1);
392850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl  new (t1, u1) int;
402850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl  delete t1;
412850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl
42cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  dummy d1 = sizeof(t1); // expected-error {{no viable conversion}}
437abfbdbc97ad8e7f340789f751df1e32b10118b4Douglas Gregor  dummy d2 = offsetof(T, foo); // expected-error {{no viable conversion}}
44cfdc81a83467973b14e4ea5e9e9af1690f135415Eli Friedman  dummy d3 = __alignof(u1); // expected-error {{no viable conversion}}
450c42bb653dc40b1caae010618831e320af824b18Chris Lattner  i1 = typeid(t1); // expected-error {{assigning to 'int' from incompatible type 'const std::type_info'}}
462850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl
472850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl  return u1;
482850784bda09416fc7e9d57f5baa36c9351c757cSebastian Redl}
499807a2e0ddb1eafb8725dbf0247271a4a694037bRichard Smith
509807a2e0ddb1eafb8725dbf0247271a4a694037bRichard Smithtemplate<typename T>
5193d6b07cd79d74e343d81c0e8fb5365376a33097Richard Smithvoid f2(__restrict T x) {} // expected-note {{substitution failure [with T = int]: restrict requires a pointer or reference ('int' is invalid}}
529807a2e0ddb1eafb8725dbf0247271a4a694037bRichard Smith
539807a2e0ddb1eafb8725dbf0247271a4a694037bRichard Smithvoid f3() {
549807a2e0ddb1eafb8725dbf0247271a4a694037bRichard Smith  f2<int*>(0);
559807a2e0ddb1eafb8725dbf0247271a4a694037bRichard Smith  f2<int>(0); // expected-error {{no matching function for call to 'f2'}}
569807a2e0ddb1eafb8725dbf0247271a4a694037bRichard Smith}
57