Connecting Tech Pros Worldwide Forums | Help | Site Map

Template function question

Steve Schlesinger
Guest
 
Posts: n/a
#1: May 3 '06
Suppose you have

class A {};
class B {}


template<class T> void SomeFcn( T t ) {…};

A a;
B b;

These are both legal:

SomeFcn( a );
SomeFcn<B>( b );

The question is: When (if ever) is the second syntax required?

// Microsoft MFC specific version of question
template<class T> void SomeArrayFcn( CArray< T, T>& array ) {…};

CArray<A,A> arraya;
CArray<B,B> arrayb;

These are both legal:

SomeArrayFcn( arraya);
SomeArrayFcn<B>( arrayb);

The question is: When (if ever) is the second syntax required?



Greg
Guest
 
Posts: n/a
#2: May 3 '06

re: Template function question


Steve Schlesinger wrote:[color=blue]
> Suppose you have
>
> class A {};
> class B {}
>
>
> template<class T> void SomeFcn( T t ) {...};
>
> A a;
> B b;
>
> These are both legal:
>
> SomeFcn( a );
> SomeFcn<B>( b );
>
> The question is: When (if ever) is the second syntax required?[/color]

I don't foresee a case in which the explict type syntax would ever be
needed in order to call SomeFcn, since the compiler can always deduce a
paramterized type from the type of the parameter passed. Of course, the
client may wish to call a different implementation of SomeFcn than the
one the compiler selects. In that case the client would have to specify
the parameterized type explicitly. But I don't foresee the syntax being
necessary, as it would, say, with a function template that accepted no
parameters.
[color=blue]
> // Microsoft MFC specific version of question
> template<class T> void SomeArrayFcn( CArray< T, T>& array ) {...};
>
> CArray<A,A> arraya;
> CArray<B,B> arrayb;
>
> These are both legal:
>
> SomeArrayFcn( arraya);
> SomeArrayFcn<B>( arrayb);
>
> The question is: When (if ever) is the second syntax required?[/color]

Again, the second syntax should never be required for this example to
compile. And again, the syntax would still be needed if the client is
not happy with the compiler's type deduction and wishes to override it.


Greg

Ivan Vecerina
Guest
 
Posts: n/a
#3: May 3 '06

re: Template function question


"Steve Schlesinger" <sschlesinger@cox.net> wrote in message
news:nxW5g.3022$_m5.232@fed1read09...
: Suppose you have
....
: template<class T> void SomeFcn( T t ) {…};
....
: These are both legal:
:
: SomeFcn( a );
: SomeFcn<B>( b );
:
: The question is: When (if ever) is the second syntax required?

Sometimes the template parameters cannot be deduced from the function
call arguments (e.g. it is a return value, or internally used).

It might also be needed to resolve ambiguities.
Consider:
template<class T> T max(T a, T b);
sample uses:
double v;
std::cin >> v;
v = max( v, 0 ); // --> Error
v = max<double>( v, 0 ); // ok
v = max( v, 0.0 ); // ok

The notation is also handy when wanting to take the address of
the function:
myFuncPtr = & max<double>;

....

hth -Ivan
--
http://ivan.vecerina.com/contact/?subject=NG_POST <- email contact form
Brainbench MVP for C++ <> http://www.brainbench.com




Aman Angrish
Guest
 
Posts: n/a
#4: May 3 '06

re: Template function question


> SomeFcn<B>( b );[color=blue]
>
> The question is: When (if ever) is the second syntax required?[/color]

Since argument deduction doesn't do any automatic type conversion,
the 2nd syntax will be needed when you want type conversion. (either to
resolve ambiguity or for "conversion")

example :
template<typename T>
void f(T t){cout << t << endl; }
int main()
{
f<int>('a'); // shows you 97
f('a'); // shows you a
}

regards,
Aman Angrish.


--
Posted via Mailgate.ORG Server - http://www.Mailgate.ORG
Stuart Redmann
Guest
 
Posts: n/a
#5: May 3 '06

re: Template function question


Steve Schlesinger wrote:[color=blue]
> Suppose you have
>
> class A {};
> class B {}
>
>
> template<class T> void SomeFcn( T t ) {…};
>
> A a;
> B b;
>
> These are both legal:
>
> SomeFcn( a );
> SomeFcn<B>( b );
>
> The question is: When (if ever) is the second syntax required?
>[/color]

As Ivan has already mentioned, it is necessary for functions that take
no arguments, or only such arguments from which the template type can't
be deduced. While such functions should be pretty rare (I'd be curious
if anybody knows such a case), you will encounter the explicit
parametrisation when you invoke member functions on objects. One case
where such explicit template parameters are necessary are cast operators
(they usually don't have arguments, so the compiler needs your advice
for deciding which member function to generate).

Regards,
Stuart
Steve Schlesinger
Guest
 
Posts: n/a
#6: May 4 '06

re: Template function question


Thanks to all who responded -

I assume it is safe to say then, that if the compiler required a class
specifier
eg SomeFcn<B>( b );
and I didn't have one, it would flag it as an error.

-Steve


"Stuart Redmann" <DerTopper@web.de> wrote in message
news:e39o57$ank$1@news.dtag.de...[color=blue]
> Steve Schlesinger wrote:[color=green]
> > Suppose you have
> >
> > class A {};
> > class B {}
> >
> >
> > template<class T> void SomeFcn( T t ) {…};
> >
> > A a;
> > B b;
> >
> > These are both legal:
> >
> > SomeFcn( a );
> > SomeFcn<B>( b );
> >
> > The question is: When (if ever) is the second syntax required?
> >[/color]
>
> As Ivan has already mentioned, it is necessary for functions that take
> no arguments, or only such arguments from which the template type can't
> be deduced. While such functions should be pretty rare (I'd be curious
> if anybody knows such a case), you will encounter the explicit
> parametrisation when you invoke member functions on objects. One case
> where such explicit template parameters are necessary are cast operators
> (they usually don't have arguments, so the compiler needs your advice
> for deciding which member function to generate).
>
> Regards,
> Stuart[/color]


Closed Thread