473,387 Members | 1,757 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.

regarding templates

Hi Everyone,

Its know that c++ compiler instantiates a new version of the
function/class when it sees a usage of a function/class template with a
particular data type, assuming that c++ supports 10 data types, how
many versions of the function template would be created for the
following example?

template <class T>
T sample(T value)
{
return T * T;
}

int main()
{
int i=5;
sample(i);
}

Would the compiler create one version of the function to handle int
data type or as many versions as many as the number of data types that
c++ supports?

Dec 12 '06 #1
5 1251
sa*****@yahoo.co.in wrote:
Its know that c++ compiler instantiates a new version of the
function/class when it sees a usage of a function/class template with
a particular data type, assuming that c++ supports 10 data types, how
many versions of the function template would be created for the
following example?

template <class T>
T sample(T value)
{
return T * T;
}

int main()
{
int i=5;
sample(i);
}

Would the compiler create one version of the function to handle int
data type or as many versions as many as the number of data types that
c++ supports?
How many times 'sample' is *used*? How many different types in all
uses of 'sample' are there? Figure that out, and you will have figured
the number of template instantiations.

V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
Dec 12 '06 #2
sa*****@yahoo.co.in wrote:
Hi Everyone,

Its know that c++ compiler instantiates a new version of the
function/class when it sees a usage of a function/class template with a
particular data type, assuming that c++ supports 10 data types,
C++ supports an (effectively) infinite number of data types.
how
many versions of the function template would be created for the
following example?

template <class T>
T sample(T value)
{
return T * T;
I assume that you meant:
return value * value;
}

int main()
{
int i=5;
sample(i);
}

Would the compiler create one version of the function to handle int
data type or as many versions as many as the number of data types that
c++ supports?
As you stated above: "Its know [sic] that c++ compiler instantiates a
new version of the function/class when it sees a usage of a
function/class template with a particular data type", so your answer is:
one.
--
Clark S. Cox III
cl*******@gmail.com
Dec 12 '06 #3
sam_...@yahoo.co.in wrote:
Its know that c++ compiler instantiates a new version of the
function/class when it sees a usage of a function/class template with a
particular data type, assuming that c++ supports 10 data types, how
many versions of the function template would be created for the
following example?

template <class T>
T sample(T value)
{
return T * T;
}

int main()
{
int i=5;
sample(i);
}

Would the compiler create one version of the function to handle int
data type or as many versions as many as the number of data types that
c++ supports?
Only the ones you use. Moreover, it could also instantiate that
template (assuming you fix the syntax errors) for user defined types,
not just built-in types:

class X { /*...*/ };
X operator( const X& x1, const X& x2 ); // defined somewhere

int main()
{
sample( 5 ); // Invoke sample<int>()
sample( 5.0 ); // Invoke sample<double>()
X x;
sample( x ); // Invoke sample<X>()
}

Cheers! --M

Dec 12 '06 #4
Template is object-level programming, that means only when the
compliler need to generate objects templatelized, such as class object,
function object.., by template, the versions(as you said) should be
created by the compliler accordingly.
So, how many distinct date types you passed to function template, how
many versions created!
"sa*****@yahoo.co.in дµÀ£º
"
Hi Everyone,

Its know that c++ compiler instantiates a new version of the
function/class when it sees a usage of a function/class template with a
particular data type, assuming that c++ supports 10 data types, how
many versions of the function template would be created for the
following example?

template <class T>
T sample(T value)
{
return T * T;
}

int main()
{
int i=5;
sample(i);
}

Would the compiler create one version of the function to handle int
data type or as many versions as many as the number of data types that
c++ supports?
Dec 12 '06 #5

sa*****@yahoo.co.in wrote:
Hi Everyone,

Its know that c++ compiler instantiates a new version of the
function/class when it sees a usage of a function/class template with a
particular data type, assuming that c++ supports 10 data types, how
many versions of the function template would be created for the
following example?

template <class T>
T sample(T value)
{
return T * T;
}

int main()
{
int i=5;
sample(i);
}

Would the compiler create one version of the function to handle int
data type or as many versions as many as the number of data types that
c++ supports?
The compiler only generates a version per type used. And you should use
references instead of returns specially if you plan to ignore returns.

#include <iostream>
#include <ostream>

template< class T >
void square(T& value)
{
value *= value;
}

int main()
{
int n = 5;
square(n);
std::cout << "n = " << n << std::endl;
}

/*
n = 25
*/

Dec 12 '06 #6

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

Similar topics

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...
3
by: darkstorm | last post by:
I have a doubt regarding inheritance involving templates Consider this: ///////////////////////////////////// template<typename T> class A { private: T m_a;
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 ]
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...
2
by: vikram_p_nayak | last post by:
This is quite fundamental. So I apologize if it has already been discussed. >From what I understand, a template declaration and definition is generally in the same file. (like the standard...
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...
4
by: noone | last post by:
Hi. I've got a fella working for me who is trying to convince me to change our coding standards to using separate .h and .cc files for definitions and implementations. I know this is a...
104
by: JohnQ | last post by:
Well apparently not since one can step thru template code with a debugger. But if I was willing to make the concession on debugging, templates would be strictly a precompiler thing? I have a...
6
by: thorsten.schilling | last post by:
Hello everybody, the question is, what is the "golden way" or the best way, if I have a memberfunction in a class, which should return a new instance of an object. For example some class Foo...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.