Clarification in templates

S

sarathy

Hi all,
I have a clarification in C++ Templates.

Consider that we have created a template as below

template <class T1,class T2, class T3>
void real_test(T1 a, T2 b ,T3 c)
{
const char *x=a;
int y=b;
double z=c;



cout << x << " " << y << " " << z << endl;
}
when this method is called from main, there are 2 ways of calling it.

real_test<const char*, int, double> ("Hello",123,34.56);
real_test("Hello",123,34.56);

Both worked fine in gcc-3.2.2 in Redhat9 i386.

But when i read the document at the following URL
http://www.cplusplus.com/doc/tutorial/templates.html

it read as

"Only when all the values passed to the template, are of the same type,
then compiler will automatically detect the type, else it wont and it
will generate an error"

ie real_test (10,20,30) will do instead of real_test
<int,int,int>(10,20,30);

ie real_test("Hello",123,34.56); wont work instead of real_test<const
char*, int, double> ("Hello",123,34.56);

Please clarify whether the statement in the URL is correct.

Regards,
Sarathy
 
V

Victor Bazarov

sarathy said:
Hi all,
I have a clarification in C++ Templates.

You have it? Or do you *need* it?
Consider that we have created a template as below

template <class T1,class T2, class T3>
void real_test(T1 a, T2 b ,T3 c)
{
const char *x=a;
int y=b;
double z=c;



cout << x << " " << y << " " << z << endl;
}
when this method is called from main, there are 2 ways of calling it.

real_test<const char*, int, double> ("Hello",123,34.56);
real_test("Hello",123,34.56);

Both worked fine in gcc-3.2.2 in Redhat9 i386.

But when i read the document at the following URL
http://www.cplusplus.com/doc/tutorial/templates.html

it read as

"Only when all the values passed to the template, are of the same
type, then compiler will automatically detect the type, else it wont
and it will generate an error"

I don't see this statement on the page you gave.
ie real_test (10,20,30) will do instead of real_test
<int,int,int>(10,20,30);
Yes.

ie real_test("Hello",123,34.56); wont work instead of real_test<const
char*, int, double> ("Hello",123,34.56);

It will. Your template *arguments* are all independent types. They
will all be deduced independently.
Please clarify whether the statement in the URL is correct.

Again, I don't see that statement in "the URL".

V
 
S

sarathy

Apology. I got it in the wrong sense from the URL. Clear now.

Thanx & Rgrds,
Sarathy
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,432
Messages
2,571,680
Members
48,796
Latest member
Greg L.

Latest Threads

Top