472,353 Members | 1,184 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,353 software developers and data experts.

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 1679
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: Dave | last post by:
Hello all, Consider this function template definition: template<typename T> void foo(T) {} If foo is never called, this template will never...
3
by: 胡岳偉(Yueh-Wei Hu) | last post by:
Hi all, I have 2 questions about template function as friends in template classes. I don't know why, and hope someone could help me. ...
0
by: Yueh-Wei Hu | last post by:
Victor Bazarov <v.Abazarov@comAcast.net> wrote in message news: ============================================================== > > Question 1: > >...
4
by: Thomi Richards | last post by:
Hi, I'm trying to create a simple stack class using C++ and templates. Everything works well and good if I amke the class totally inline....
5
by: wongjoekmeu | last post by:
Hello all, I have a question about templates. Let say I have the following template function ----- template <class T> T max(T a, T b) {...
2
by: pookiebearbottom | last post by:
Just trying to learn some things about templates. Was wondering how boost::tupple really works, but the headers were a bit confusing to me. I know...
5
by: StephQ | last post by:
This is from a thread that I posted on another forum some days ago. I didn't get any response, so I'm proposing it in this ng in hope of better...
6
by: Bartholomew Simpson | last post by:
I'm trying to avoid (or at least, minimize) duplicity of effort. I have the following function: void Permissions::Exists(const unsigned int id,...
4
by: David Sanders | last post by:
Hi, I have a class with an integer template parameter, taking values 1, 2 or 3, and a function 'calc' in that class which performs...
21
by: H9XLrv5oXVNvHiUI | last post by:
Hi, I have a question about injecting friend functions within template classes. My question is specific to gcc (version 3.4.5) used in combination...
1
by: Kemmylinns12 | last post by:
Blockchain technology has emerged as a transformative force in the business world, offering unprecedented opportunities for innovation and...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...
0
by: Matthew3360 | last post by:
Hi, I have been trying to connect to a local host using php curl. But I am finding it hard to do this. I am doing the curl get request from my web...
0
Oralloy
by: Oralloy | last post by:
Hello Folks, I am trying to hook up a CPU which I designed using SystemC to I/O pins on an FPGA. My problem (spelled failure) is with the...
0
by: Carina712 | last post by:
Setting background colors for Excel documents can help to improve the visual appeal of the document and make it easier to read and understand....

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.