470,619 Members | 1,483 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,619 developers. It's quick & easy.

Template function question

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?
May 3 '06 #1
5 1591
Steve Schlesinger wrote:
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?
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.
// 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?


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

May 3 '06 #2
"Steve Schlesinger" <ss**********@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


May 3 '06 #3
> SomeFcn<B>( b );

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


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
May 3 '06 #4
Steve Schlesinger wrote:
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?


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
May 3 '06 #5
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" <De*******@web.de> wrote in message
news:e3**********@news.dtag.de...
Steve Schlesinger wrote:
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?


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

May 4 '06 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by 胡岳偉(Yueh-Wei Hu) | last post: by
reply views Thread by Yueh-Wei Hu | last post: by
4 posts views Thread by Thomi Richards | last post: by
5 posts views Thread by wongjoekmeu | last post: by
6 posts views Thread by Bartholomew Simpson | last post: by
4 posts views Thread by David Sanders | last post: by
21 posts views Thread by H9XLrv5oXVNvHiUI | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.