xref: /llvm-project/clang/test/SemaTemplate/member-function-template.cpp (revision 7dbfb4616377442f6424f903b045b70c29467630)
18fbe78f6SDaniel Dunbar // RUN: %clang_cc1 -fsyntax-only -verify %s
23447e767SDouglas Gregor 
33447e767SDouglas Gregor struct X {
497628d6aSDouglas Gregor   template<typename T> T& f0(T);
597628d6aSDouglas Gregor 
g0X697628d6aSDouglas Gregor   void g0(int i, double d) {
797628d6aSDouglas Gregor     int &ir = f0(i);
897628d6aSDouglas Gregor     double &dr = f0(d);
997628d6aSDouglas Gregor   }
1097628d6aSDouglas Gregor 
1197628d6aSDouglas Gregor   template<typename T> T& f1(T);
1297628d6aSDouglas Gregor   template<typename T, typename U> U& f1(T, U);
1397628d6aSDouglas Gregor 
g1X1497628d6aSDouglas Gregor   void g1(int i, double d) {
1597628d6aSDouglas Gregor     int &ir1 = f1(i);
1697628d6aSDouglas Gregor     int &ir2 = f1(d, i);
1797628d6aSDouglas Gregor     int &ir3 = f1(i, i);
1897628d6aSDouglas Gregor   }
193447e767SDouglas Gregor };
2097628d6aSDouglas Gregor 
test_X_f0(X x,int i,float f)2197628d6aSDouglas Gregor void test_X_f0(X x, int i, float f) {
2297628d6aSDouglas Gregor   int &ir = x.f0(i);
2397628d6aSDouglas Gregor   float &fr = x.f0(f);
2497628d6aSDouglas Gregor }
2597628d6aSDouglas Gregor 
test_X_f1(X x,int i,float f)2697628d6aSDouglas Gregor void test_X_f1(X x, int i, float f) {
2797628d6aSDouglas Gregor   int &ir1 = x.f1(i);
2897628d6aSDouglas Gregor   int &ir2 = x.f1(f, i);
2997628d6aSDouglas Gregor   int &ir3 = x.f1(i, i);
3097628d6aSDouglas Gregor }
31da21f27eSDouglas Gregor 
test_X_f0_address()32da21f27eSDouglas Gregor void test_X_f0_address() {
33da21f27eSDouglas Gregor   int& (X::*pm1)(int) = &X::f0;
34da21f27eSDouglas Gregor   float& (X::*pm2)(float) = &X::f0;
35da21f27eSDouglas Gregor }
36da21f27eSDouglas Gregor 
test_X_f1_address()37da21f27eSDouglas Gregor void test_X_f1_address() {
38da21f27eSDouglas Gregor   int& (X::*pm1)(int) = &X::f1;
39da21f27eSDouglas Gregor   float& (X::*pm2)(float) = &X::f1;
40da21f27eSDouglas Gregor   int& (X::*pm3)(float, int) = &X::f1;
41da21f27eSDouglas Gregor }
42c45a40afSDouglas Gregor 
test_X_f0_explicit(X x,int i,long l)43fbc18234SDouglas Gregor void test_X_f0_explicit(X x, int i, long l) {
44fbc18234SDouglas Gregor   int &ir1 = x.f0<int>(i);
45fbc18234SDouglas Gregor   int &ir2 = x.f0<>(i);
4684f14dd6SDouglas Gregor   long &il1 = x.f0<long>(i);
47fbc18234SDouglas Gregor }
48fbc18234SDouglas Gregor 
49c45a40afSDouglas Gregor // PR4608
a(x z)50c45a40afSDouglas Gregor class A { template <class x> x a(x z) { return z+y; } int y; };
51b7bfe794SDouglas Gregor 
52358e7745SDouglas Gregor // PR5419
53358e7745SDouglas Gregor struct Functor {
54358e7745SDouglas Gregor   template <typename T>
operator ()Functor55358e7745SDouglas Gregor   bool operator()(const T& v) const {
56358e7745SDouglas Gregor     return true;
57358e7745SDouglas Gregor   }
58358e7745SDouglas Gregor };
59358e7745SDouglas Gregor 
test_Functor(Functor f)60358e7745SDouglas Gregor void test_Functor(Functor f) {
61358e7745SDouglas Gregor   f(1);
62358e7745SDouglas Gregor }
633fad6178SDouglas Gregor 
643fad6178SDouglas Gregor // Instantiation on ->
653fad6178SDouglas Gregor template<typename T>
663fad6178SDouglas Gregor struct X1 {
673fad6178SDouglas Gregor   template<typename U> U& get();
683fad6178SDouglas Gregor };
693fad6178SDouglas Gregor 
703fad6178SDouglas Gregor template<typename T> struct X2; // expected-note{{here}}
713fad6178SDouglas Gregor 
test_incomplete_access(X1<int> * x1,X2<int> * x2)723fad6178SDouglas Gregor void test_incomplete_access(X1<int> *x1, X2<int> *x2) {
733fad6178SDouglas Gregor   float &fr = x1->get<float>();
743fad6178SDouglas Gregor   (void)x2->get<float>(); // expected-error{{implicit instantiation of undefined template}}
753fad6178SDouglas Gregor }
76b9397108SDouglas Gregor 
77b9397108SDouglas Gregor // Instantiation of template template parameters in a member function
78b9397108SDouglas Gregor // template.
79b9397108SDouglas Gregor namespace TTP {
80b9397108SDouglas Gregor   template<int Dim> struct X {
81b9397108SDouglas Gregor     template<template<class> class M, class T> void f(const M<T>&);
82b9397108SDouglas Gregor   };
83b9397108SDouglas Gregor 
84b9397108SDouglas Gregor   template<typename T> struct Y { };
85b9397108SDouglas Gregor 
test_f(X<3> x,Y<int> y)86b9397108SDouglas Gregor   void test_f(X<3> x, Y<int> y) { x.f(y); }
87b9397108SDouglas Gregor }
88*7dbfb461SDouglas Gregor 
89*7dbfb461SDouglas Gregor namespace PR7387 {
90*7dbfb461SDouglas Gregor   template <typename T> struct X {};
91*7dbfb461SDouglas Gregor 
92*7dbfb461SDouglas Gregor   template <typename T1> struct S {
93*7dbfb461SDouglas Gregor     template <template <typename> class TC> void foo(const TC<T1>& arg);
94*7dbfb461SDouglas Gregor   };
95*7dbfb461SDouglas Gregor 
96*7dbfb461SDouglas Gregor   template <typename T1> template <template <typename> class TC>
foo(const TC<T1> & arg)97*7dbfb461SDouglas Gregor   void S<T1>::foo(const TC<T1>& arg) {}
98*7dbfb461SDouglas Gregor 
test(const X<int> & x)99*7dbfb461SDouglas Gregor   void test(const X<int>& x) {
100*7dbfb461SDouglas Gregor     S<int> s;
101*7dbfb461SDouglas Gregor     s.foo(x);
102*7dbfb461SDouglas Gregor   }
103*7dbfb461SDouglas Gregor }
104