|
-
April 15th, 2008, 12:14 AM
#1
function template
im working on a function template. i just want to know if im doing it right so far. i put comments on parts im not sure of
#include<iostream>
using namespace std;
template <typename isEqualTo = int> // is this a template header with one formal type of parameter
// how would i write a header for function template isEqualTo
{
return arg1==arg2;
}
int main(){
int a;
int b;
cout<<"enter two interger values: ";
cin>>a>>b;
cout<<a<<"and"<<b<<"are"
<<( ?"equal":"not equal")<<'\n';
return 0;
}
-
April 15th, 2008, 02:21 AM
#2
Re: function template
I think you have this in mind
Code:
template <typename T = int>
bool AreEqual(const T arg1, const T arg2)
{
return arg1==arg2;
}
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
April 15th, 2008, 09:33 AM
#3
Re: function template
 Originally Posted by souldog
I think you have this in mind
Code:
template <typename T = int>
bool AreEqual(const T arg1, const T arg2)
{
return arg1==arg2;
}
You can't have default template arguments with "function" templates. Making that a functor might solve the problem though.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
April 15th, 2008, 09:53 AM
#4
Re: function template
 Originally Posted by exterminator
You can't have default template arguments with "function" templates. Making that a functor might solve the problem though. 
I can't imagine the purpose of a default template argument in this case anyway, since the type appears in the parameter list of the function and can be automatically deduced.
The point about using a functor is a good one, though, especially considering STL already provides such a functor: std::equal_to<T>
- Alon
-
April 15th, 2008, 11:53 AM
#5
Re: function template
 Originally Posted by Hermit
I can't imagine the purpose of a default template argument in this case anyway, since the type appears in the parameter list of the function and can be automatically deduced.
The point about using a functor is a good one, though, especially considering STL already provides such a functor: std::equal_to<T>
That's correct. I was just considering that it was for illustration purpose as an example of how to write a function template taking a type argument.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
April 15th, 2008, 12:52 PM
#6
Re: function template
 Originally Posted by exterminator
You can't have default template arguments with "function" templates. Making that a functor might solve the problem though. 
Yah, that is what I get for copy and paste without thinking
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
-
April 15th, 2008, 06:21 PM
#7
Re: function template
well i get an error saying that its only allowed using a class template. is the only way to use a template is thru a class?
also would that template work for both char and doubles?
or am i getting that error because i havent called the template in the main?
Code:
#include<iostream>
using namespace std;
template <typename T = int>
bool AreEqual(const T arg1, const T arg2){
return arg1==arg2;
}
int main(){
int a;
int b;
cout<<"enter two interger values: ";
cin>>a>>b;
cout<<a<<"and"<<b<<"are"
<<( ?"equal":"not equal")<<'\n';
char c;
char d;
cout<<"\nenter two character values: ";
cin>>c>>d;
cout<<c<<"and"<<d<<"are"
<<( ?"equal":"not equal")<<'\n';
double e;
double f;
cout<<"\nenter two double values: ";
cin>>e>>f;
cout<<e<<"and"<<f<<"are"
<<( ?"equal":"not equal")<<'\n';
return 0;
}
Last edited by ptg; April 15th, 2008 at 06:23 PM.
-
April 15th, 2008, 11:27 PM
#8
Re: function template
 Originally Posted by souldog
Yah, that is what I get for copy and paste without thinking
Good to see you, one of the old-timers, back!
Last edited by exterminator; April 16th, 2008 at 12:57 AM.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
April 16th, 2008, 12:56 AM
#9
Re: function template
Code:
template <typename T = int>
bool AreEqual(const T arg1, const T arg2){
return arg1==arg2;
}
change above to:
Code:
template <typename T>
bool AreEqual(const T& arg1, const T& arg2){ //change to reference if complex types might be compared as well
return arg1==arg2;
}
Or you could simply use std::equal_to<T> as suggested by Hermit. Follow the link he posted.
Can you help me with my homework assignment?, Before you post!, Use code tags, How to post!, Codeguru technical FAQs, C++ FAQ Lite, Stroustrup: C++ Style and Technique FAQ, Guru of the Week, Comeau C and C++ FAQs, Comeau C++ Templates FAQs, CUJ @ DDJ, Spam threshold
My Blogs : Learning C++ is fun | Abnegator's reflections
Open Threads : C++ Aha! Moments | Nature of work in C++?
-
April 16th, 2008, 02:23 AM
#10
Re: function template
 Originally Posted by exterminator
 Good to see you, one of the old-timers, back!
Thanks. Nice to be back
Wakeup in the morning and kick the day in the teeth!! Or something like that.
"i don't want to write leak free code or most efficient code, like others traditional (so called expert) coders do."
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|