explicit template arguments to function templates 
Author Message
 explicit template arguments to function templates

According to my Visual C++ 6.0 compiler, and nothing to contradict it in
Stroustrup, it is not legal to explicitly specify template arguments when
calling a member function template. However, it is legal for global function
templates.

For instance, this is legal:

long l = GlobalFunction<CMyClass>(cobj);

but this is not:

long l = myObject.MemberFunction<CMyClass>(cobj); // Compiler Error C2275

Can anyone explain why? It seems a strange constraint.

Steve White



Thu, 12 Feb 2004 02:33:26 GMT  
 explicit template arguments to function templates

Quote:

>According to my Visual C++ 6.0 compiler, and nothing to contradict it in
>Stroustrup, it is not legal to explicitly specify template arguments when
>calling a member function template. However, it is legal for global function
>templates.

>For instance, this is legal:

>long l = GlobalFunction<CMyClass>(cobj);

>but this is not:

>long l = myObject.MemberFunction<CMyClass>(cobj); // Compiler Error C2275

>Can anyone explain why? It seems a strange constraint.

VC6 simply doesn't support that legal syntax. To use the global
function form successfully, be sure to read KB article Q240871, but
instead of the suggested solution:

template<class T>
void f(T dummy = 0)

use:

template<class T>
void f(T* = 0)

The latter is generalized and more efficient.

--
Doug Harrison [VC++ MVP]
Eluent Software, LLC
http://www.eluent.com
Tools for Visual C++ and Windows



Thu, 12 Feb 2004 05:11:34 GMT  
 explicit template arguments to function templates
On Sat, 25 Aug 2001 19:33:26 +0100, "Steve White"

Quote:

>According to my Visual C++ 6.0 compiler, and nothing to contradict it in
>Stroustrup, it is not legal to explicitly specify template arguments when
>calling a member function template. However, it is legal for global function
>templates.

>For instance, this is legal:

>long l = GlobalFunction<CMyClass>(cobj);

>but this is not:

>long l = myObject.MemberFunction<CMyClass>(cobj); // Compiler Error C2275

>Can anyone explain why? It seems a strange constraint.

In addition to Doug's response, there is one weirdness with explicitly
calling member templates where the template has dependent template
arguments. The you have to make a special use of the "template"
keyword:

(Doesn't work on MSVC6 of course)

void f()
{
long l = myObject.MemberFunction<CMyClass>(cobj);  //fine

Quote:
}

template <class T, int val>
void f2(T& myObject)
{
long l = myObject.MemberFunction<val>(cobj);  //not fine
//was the < the start of a template parameter or a less-than sign?

long l = myObject.template MemberFunction<val>(cobj); //fine
//the template keyword helps the poor compiler out, disambiguating
//the <.

Quote:
}

Tom


Thu, 12 Feb 2004 22:30:52 GMT  
 explicit template arguments to function templates
Thanks for the replies, chaps. I know that VC++.NET 7 beta 2 has many
compiler improvements and better ANSI compliance. I don't know if this
aspect is now supported but I shall enjoy finding out.

Steve White



Sun, 15 Feb 2004 23:14:47 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. template arguments used within a template function - how?

2. Bug with explicit function template instantiation in VS.NET

3. "member function templates with explicit specification"

4. explicit specialization of class template member function

5. explicit template function problem

6. Explicit instantiation of member function template

7. Template parameter conflicts with typedef in template argument

8. Compiler barf on template explicit function instantiation.

9. explicit member function template instantiation

10. explicit instantiation of a template function?

11. Compiler barf on template explicit function instantiation.

12. explicit template type for member functions

 

 
Powered by phpBB® Forum Software