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

generic functions(template functions)

Hello Experts!

Assume I have one function that is a template function called add and one
concrete function that also have the name add is it any point to make a
concrete function add in this example?

template <typename T>
T add(T rh, T lh)
{ return rh+lh; }

template <typename T>
int add(int rh, int lh)
{ return rh+lh; }
int main()
{
int i=1, j=2;
cout << "result " << add<int>(i,j) << endl;

return 0;
}

//Many thanks

//Tony
Aug 16 '05 #1
3 1505
> Hello Experts!

Assume I have one function that is a template function called add and one
concrete function that also have the name add is it any point to make a
concrete function add in this example?

template <typename T>
T add(T rh, T lh)
{ return rh+lh; }

template <typename T>
int add(int rh, int lh)
{ return rh+lh; }
int main()
{
int i=1, j=2;
cout << "result " << add<int>(i,j) << endl;
return 0;

}


In the above example there's no point for the template
specialization(That's the term you'd want to use). In general you'd
want to specialize a function template when you want to have different
behavior for particular types.

Srini

Aug 16 '05 #2

"Srini" <sr*********@gmail.com> wrote in message
news:11**********************@g47g2000cwa.googlegr oups.com...
Hello Experts!

Assume I have one function that is a template function called add and one
concrete function that also have the name add is it any point to make a
concrete function add in this example?

template <typename T>
T add(T rh, T lh)
{ return rh+lh; }

template <typename T>
int add(int rh, int lh)
{ return rh+lh; }
int main()
{
int i=1, j=2;
cout << "result " << add<int>(i,j) << endl;
return 0;

}


In the above example there's no point for the template
specialization(That's the term you'd want to use). In general you'd
want to specialize a function template when you want to have different
behavior for particular types.

Srini


The code by Tony didn't look to me like template specialization. A full
specialization of function template add<> would look like:

template <>
int add(int rh, int lh)
{
return rh + lh;
}
Aug 16 '05 #3
> The code by Tony didn't look to me like template specialization. A full
specialization of function template add<> would look like:

template <>
int add(int rh, int lh)
{
return rh + lh;
}


Yeah - even I thought so. But I checked compiling both forms (What OP
has given and what you've given) and both gave same results. I thought
that specialization was what the OP meant in his post.

Aug 16 '05 #4

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

Similar topics

3
by: Jim Newton | last post by:
hi all, i'm relatively new to python. I find it a pretty interesting language but also somewhat limiting compared to lisp. I notice that the language does provide a few lispy type nicities, but...
1
by: bluekite2000 | last post by:
Here is the compilable code, along w/ the error #include<iostream> #include<complex> typedef std::complex<float> ComplexSingle; using namespace std; template<typename T> class X { private:...
19
by: Celine | last post by:
I have a set of similar classes (c1, c2, ...). I would like to create a template (or a page with as much abstraction as possible) to copy/paste, and create ASPX pages to edit (add/update/...)...
1
by: Celine | last post by:
I still failed to pass DataTypes as parameters, as in the following 3 examples: this.mainObject = (mType)Session; this.mainObjectDt.AddMainObjectRow(this.DataSet_GUIDs_C1Row);...
0
by: Celine | last post by:
I still failed to pass DataTypes as parameters, as in the following 3 examples: .... mType = typeof(Class1); this.mainObject = (mType)Session;...
28
by: steve yee | last post by:
i think c should adapt c++ template standard, as well as namespace. if so, c can replace c++ in many cases.
4
by: shuisheng | last post by:
Dear All, Would you please help me to look at this simple case: Assume I have a function to calculate the power of base ^ order. Here base can be of any proper type, and order of type size_t....
3
by: gary.bernstein | last post by:
I want to call a singleton getInstance function to retrieve a templatized object without knowing what types were used to create the singleton object in the first call to getInstance. How can I do...
3
by: wquatan | last post by:
I would to know if it is possible to write generic functions to set properties on form.fields A quick example to show what I mean : Instead of Me!FieldA.BackColor = 255 Me!FieldB.BackColor =...
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:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
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: 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
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.