calling template member function in template class 
Author Message
 calling template member function in template class

Hi all, hopefully there is a work around for this problem.

I'm trying to call a template member function in a template class and am not
having any luck.

I've tested the following code on:
http://www.*-*-*.com/
and it compiles fine - however MVC 6.0 doesn't seem capable of doing it.

class Object {};
class Test : Object {};

template <class T>
class SmartPtr{
  public:

      template <class U>
      int foo (){
        return 435;
    }

Quote:
};

int main (){
  SmartPtr<Test> testPtr;
  testPtr.foo <Object>();

Quote:
}

------------------------------------------
Can anyone tell me if there is a work-around in C++ so that MVC 6.0 (service
patch 5) can actually do the above or something similar?

Obviously in this case, I don't actually need the class in the foo
function - but this is a dumbed down version of what I would like to get
working.  I.E. I tried to reduce the problem I'm having down to the basics -
that is the requirement for a template function in a template class.

Thanks,
Novice

PS When I attempt to do the above in MVC it gives a compiler error of:
c:\tim\c++programs\timdynamic\mainentry.cpp(30) : error C2275: 'Object' :
illegal use of this type as an expression
        c:\tim\c++programs\timdynamic\mainentry.cpp(14) : see declaration of
'Object'
c:\tim\c++programs\timdynamic\mainentry.cpp(30) : error C2059: syntax error
: ')'



Mon, 28 Nov 2005 05:10:17 GMT  
 calling template member function in template class

Quote:

>Hi all, hopefully there is a work around for this problem.

>I'm trying to call a template member function in a template class and am not
>having any luck.

>I've tested the following code on:
>http://www.comeaucomputing.com/tryitout/
>and it compiles fine - however MVC 6.0 doesn't seem capable of doing it.

>class Object {};
>class Test : Object {};

>template <class T>
>class SmartPtr{
>  public:

>      template <class U>
>      int foo (){
>        return 435;
>    }

>};

>int main (){
>  SmartPtr<Test> testPtr;
>  testPtr.foo <Object>();
>}

>------------------------------------------
>Can anyone tell me if there is a work-around in C++ so that MVC 6.0 (service
>patch 5) can actually do the above or something similar?

VC6 doesn't support that syntax. A workaround would be:

 template<class U> int foo(U*) { return 435; }

This you can call with:

 testPtr.foo((Object*) 0);

--
Doug Harrison
Microsoft MVP - Visual C++



Mon, 28 Nov 2005 05:26:10 GMT  
 calling template member function in template class
Quote:
> class Object {...};
> class Test : Object {...};

> template <class T>
> class SmartPtr{
>   public:

      template <class U>
        int foo (const SmartPtr<T>& _smartPtr, U * _u){
          return 345;
     }
Quote:

> };

> int main (){
>   SmartPtr<Object> objectPtr;
>   SmartPtr<Test> testPtr;

   objectPtr.foo <Test>(objectPtr, (U *) 0);

Quote:
> }

Nevermind, I just realized how the mechanism worked for MVC 6 (see above).

Thanks,
Novice



Mon, 28 Nov 2005 07:24:31 GMT  
 calling template member function in template class


Quote:

> VC6 doesn't support that syntax. A workaround would be:

>  template<class U> int foo(U*) { return 435; }

> This you can call with:

>  testPtr.foo((Object*) 0);

What if I want to pass another argument of a different type into the
function, i.e.:

class Object {...};
class Test : Object {...};

template <class T>
class SmartPtr{
  public:
     template <class U>
       int foo (const SmartPtr<T>& _smartPtr){
         return 345;
    }

Quote:
};

int main (){
  SmartPtr<Object> objectPtr;
  SmartPtr<Test> testPtr;
  objectPtr.foo <Test>(objectPtr);

Quote:
}

Thanks very much,
Novice
Quote:
> --
> Doug Harrison
> Microsoft MVP - Visual C++



Mon, 28 Nov 2005 07:16:58 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. calling template member function in template class

2. template class with a template member function

3. Template member functions of template classes

4. Function template as member in a class template

5. template member function - non template class - VC5 - error

6. Simple question: Member function templates in template classes under VC++ 5.0

7. Using member function templates in template class under VC++ 5.0

8. template functions in template classes declared outside the class definition

9. Template function within Template Class

10. pb with template function in non template class

11. How to call template member functions?

12. template function in a template class

 

 
Powered by phpBB® Forum Software