Click to See Complete Forum and Search --> : function template


ptg
April 15th, 2008, 12:14 AM
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;
}

souldog
April 15th, 2008, 02:21 AM
I think you have this in mind


template <typename T = int>
bool AreEqual(const T arg1, const T arg2)
{
return arg1==arg2;
}

exterminator
April 15th, 2008, 09:33 AM
I think you have this in mind


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. :)

Hermit
April 15th, 2008, 09:53 AM
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> (http://www.sgi.com/tech/stl/equal_to.html)

exterminator
April 15th, 2008, 11:53 AM
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> (http://www.sgi.com/tech/stl/equal_to.html)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. :)

souldog
April 15th, 2008, 12:52 PM
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

ptg
April 15th, 2008, 06:21 PM
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?

#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;
}

exterminator
April 15th, 2008, 11:27 PM
Yah, that is what I get for copy and paste without thinking:) Good to see you, one of the old-timers, back!

exterminator
April 16th, 2008, 12:56 AM
template <typename T = int>
bool AreEqual(const T arg1, const T arg2){
return arg1==arg2;
}
change above to:

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.

souldog
April 16th, 2008, 02:23 AM
:) Good to see you, one of the old-timers, back!
Thanks. Nice to be back