@@ -325,6 +325,7 @@ class TestSimplifyTemplate : public TestFixture {
325325 TEST_CASE (templateTypeDeductionArray); // array arguments: only the decayed dimension is a pointer
326326 TEST_CASE (templateTypeDeductionConstPointer); // "const T *": the parameter provides the pointee const
327327 TEST_CASE (templateTypeDeductionFunctionPointer); // pointers to functions and to members are not deduced
328+ TEST_CASE (templateTypeDeductionMemberCall); // "obj . f ( x )": lookup in the class of the object, not at the call site
328329
329330 TEST_CASE (simplifyTemplateArgs1);
330331 TEST_CASE (simplifyTemplateArgs2);
@@ -7868,6 +7869,63 @@ class TestSimplifyTemplate : public TestFixture {
78687869 ASSERT_EQUALS (" " , errout_str ());
78697870 }
78707871
7872+ void templateTypeDeductionMemberCall ()
7873+ {
7874+ { // a free template with the same name and arity is not what "r . check ( p )" calls
7875+ const char code[] = " template<class T> void check(T* x) { (void)x; }\n "
7876+ " struct Range { template<class T> bool check(T x) { return x != 1000; } };\n "
7877+ " bool g(Range& r, unsigned char* p) { return r.check(p); }" ;
7878+ const char exp[] = " template < class T > void check ( T * x ) { ( void ) x ; } "
7879+ " struct Range { bool check<unsignedchar*> ( unsigned char * x ) ; } ; "
7880+ " bool g ( Range & r , unsigned char * p ) { return r . check<unsignedchar*> ( p ) ; } "
7881+ " bool Range :: check<unsignedchar*> ( unsigned char * x ) { return x != 1000 ; }" ;
7882+ ASSERT_EQUALS (exp, tok (code));
7883+ ASSERT_EQUALS (" " , errout_str ());
7884+ }
7885+ { // a literal argument: the free template is not considered either
7886+ const char code[] = " template<class T> void check(T* x) { (void)x; }\n "
7887+ " struct Range { template<class T> bool check(T x) { return x != 1000; } };\n "
7888+ " bool g(Range& r) { return r.check(5); }" ;
7889+ const char exp[] = " template < class T > void check ( T * x ) { ( void ) x ; } "
7890+ " struct Range { bool check<int> ( int x ) ; } ; "
7891+ " bool g ( Range & r ) { return r . check<int> ( 5 ) ; } "
7892+ " bool Range :: check<int> ( int x ) { return x != 1000 ; }" ;
7893+ ASSERT_EQUALS (exp, tok (code));
7894+ ASSERT_EQUALS (" " , errout_str ());
7895+ }
7896+ { // the type of the object is unknown: nothing is deduced, the call is not rewritten
7897+ const char code[] = " template<class T> T f(T x) { return 10 / x; }\n "
7898+ " int g(Unknown& o) { int a = 0; return o.f(a); }" ;
7899+ const char exp[] = " template < class T > T f ( T x ) { return 10 / x ; } "
7900+ " int g ( Unknown & o ) { int a ; a = 0 ; return o . f ( a ) ; }" ;
7901+ ASSERT_EQUALS (exp, tok (code));
7902+ ASSERT_EQUALS (" " , errout_str ());
7903+ }
7904+ { // object, pointer, dereferenced pointer, smart pointer, derived object: the member template is found
7905+ const char code[] = " struct P { template<class T> int f(T x) { return x; } };\n "
7906+ " struct Q : P {};\n "
7907+ " int g(P& p, P* q, std::shared_ptr<P> sp, Q& d, int* ip) {\n "
7908+ " return p.f(ip) + q->f(ip) + (*q).f(ip) + sp->f(ip) + d.f(ip);\n "
7909+ " }" ;
7910+ const char exp[] = " struct P { int f<int*> ( int * x ) ; } ; "
7911+ " struct Q : P { } ; "
7912+ " int g ( P & p , P * q , std :: shared_ptr < P > sp , Q & d , int * ip ) { "
7913+ " return p . f<int*> ( ip ) + q . f<int*> ( ip ) + ( * q ) . f<int*> ( ip ) + sp . f<int*> ( ip ) + d . P :: f<int*> ( ip ) ; "
7914+ " } "
7915+ " int P :: f<int*> ( int * x ) { return x ; }" ;
7916+ ASSERT_EQUALS (exp, tok (code));
7917+ ASSERT_EQUALS (" " , errout_str ());
7918+ }
7919+ { // a container is not a class with member templates; a smart pointer's own members are not deduced
7920+ const char code[] = " template<class T> int f(T x) { return x; }\n "
7921+ " int g(std::vector<int>& v, std::shared_ptr<int> sp, int* ip) { return v.f(ip) + sp.f(ip); }" ;
7922+ const char exp[] = " template < class T > int f ( T x ) { return x ; } "
7923+ " int g ( std :: vector < int > & v , std :: shared_ptr < int > sp , int * ip ) { return v . f ( ip ) + sp . f ( ip ) ; }" ;
7924+ ASSERT_EQUALS (exp, tok (code));
7925+ ASSERT_EQUALS (" " , errout_str ());
7926+ }
7927+ }
7928+
78717929 void simplifyTemplateArgs1 () {
78727930 ASSERT_EQUALS (" foo<2> = 2 ; foo<2> ;" , tok (" template<int N> foo = N; foo < ( 2 ) >;\n " ));
78737931 ASSERT_EQUALS (" foo<2> = 2 ; foo<2> ;" , tok (" template<int N> foo = N; foo < 1 + 1 >;\n " ));
0 commit comments