473,387 Members | 1,592 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Clarification in templates

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

Jul 30 '06 #1
2 1233
sarathy wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 30 '06 #2

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

Thanx & Rgrds,
Sarathy

Victor Bazarov wrote:
sarathy wrote:
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
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Jul 30 '06 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Karthik D | last post by:
Hello All, I am a newbie to C++ code particularly templates.Below is a template class defintion and I couldn't find the ObjPtr/ObjFnPtr class definition anywhere in source code. template<class...
5
by: Tom Alsberg | last post by:
Hi there... I'm recently trying to get a bit acquainted with XML Schemas and XSL. Now, I have a few questions about XSL stylesheets and templates: * Is there a way to "enter" a child element...
22
by: E. Robert Tisdale | last post by:
According to the C++ FAQ Lite: http://www.parashift.com/ What is "genericity"? Yet another way to say, "class templates." Not to be confused with "generality" (which just means avoiding...
7
by: Rakesh | last post by:
Hi, I was writing this C++ program wherein I used an include statement like - #include <iostream.h> I was told by my co-worker that this form of including a file is deprecated and should...
16
by: WittyGuy | last post by:
Hi, What is the major difference between function overloading and function templates? Thanks! http://www.gotw.ca/resources/clcm.htm for info about ]
3
by: Frankie Montenegro | last post by:
Hi everyone, I must say that, even though I think that Accelerated C++ by Koenig and Moo is an awesome text, the wording of exercises is very poor. I spend half the time just trying to figure...
8
by: Sai Kit Tong | last post by:
In the article, the description for "Modiy DLL That Contains Consumers That Use Managed Code and DLL Exports or Managed Entry Points" suggests the creation of the class ManagedWrapper. If I...
25
by: Ted | last post by:
I'm putting the posts that follow here (hopefully they will follow here!) because they were rejected in comp.lang.c++.moderated. It behooves anyone reading them to first read the the thread of the...
28
by: NewToCPP | last post by:
Hi, I am just trying to find out if there is any strong reason for not using Templates. When we use Templates it is going to replicate the code for different data types, thus increasing the...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

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.