Connecting Tech Pros Worldwide Forums | Help | Site Map

standard C operator as parameter to template function

Klaus Schneider
Guest
 
Posts: n/a
#1: Jun 22 '07
Dear newsgroup,

I want to call a template function with a standard operator (+, /, -, * for
double) as parameter. Here is a minimal example:

template <typename T>
T opIf(T val1, T val2, T (*op)(T,T) )
{
if (val2 != 0)
return op(val1, val2);
else
return (T)0;
}

How can I call this function with e.g. the standard / operator?

double res = opIf(10., 5., operator/);
or similar does not work.

Thanks a lot,
Klaus

--
Please insert my name and a dot before my email address.


Obnoxious User
Guest
 
Posts: n/a
#2: Jun 22 '07

re: standard C operator as parameter to template function


On Fri, 22 Jun 2007 11:31:11 +0200, Klaus Schneider wrote:
Quote:
Dear newsgroup,
>
I want to call a template function with a standard operator (+, /, -, * for
double) as parameter. Here is a minimal example:
>
template <typename T>
T opIf(T val1, T val2, T (*op)(T,T) )
{
if (val2 != 0)
return op(val1, val2);
else
return (T)0;
}
>
How can I call this function with e.g. the standard / operator?
>
double res = opIf(10., 5., operator/);
or similar does not work.
>
Thanks a lot,
Klaus
>
template <typename T, typename Functor>
T opIf(T val1, T val2, Functor op )
{
if (val2 != 0)
return op(val1, val2);
else
return (T)0;
}

#include <functional>
#include <iostream>

int main()
{
std::cout<<opIf(6,3,std::divides<int>())<<std::end l;
std::cout<<opIf(6,3,std::multiplies<int>())<<std:: endl;
std::cout<<opIf(6,3,std::plus<int>())<<std::endl;
std::cout<<opIf(6,3,std::minus<int>())<<std::endl;
return 0;
}

--
Obnoxious User
Kai-Uwe Bux
Guest
 
Posts: n/a
#3: Jun 22 '07

re: standard C operator as parameter to template function


Klaus Schneider wrote:
Quote:
Dear newsgroup,
>
I want to call a template function with a standard operator (+, /, -, *
for double) as parameter. Here is a minimal example:
>
template <typename T>
T opIf(T val1, T val2, T (*op)(T,T) )
{
if (val2 != 0)
return op(val1, val2);
else
return (T)0;
}
>
How can I call this function with e.g. the standard / operator?
>
double res = opIf(10., 5., operator/);
or similar does not work.
What about:



template < typename T, typename Op >
T opIf ( T lhs, T rhs, Op op ) {
if ( rhs != T() ) {
return ( op( lhs, rhs ) );
} else {
return ( T() );
}
}

#include <iostream>

int main ( void ) {
std::cout << opIf( 10.0, 5.0, std::divides<double>() ) << '\n';
}



Best

Kai-Uwe Bux
lei.bobby@gmail.com
Guest
 
Posts: n/a
#4: Jun 22 '07

re: standard C operator as parameter to template function


On 6 22 , 5 31 , Klaus Schneider <schnei...@iup.uni-heidelberg.de>
wrote:
Quote:
Dear newsgroup,
>
I want to call a template function with a standard operator (+, /, -, * for
double) as parameter. Here is a minimal example:
>
template <typename T>
T opIf(T val1, T val2, T (*op)(T,T) )
{
if (val2 != 0)
return op(val1, val2);
else
return (T)0;
>
}
>
How can I call this function with e.g. the standard / operator?
>
double res = opIf(10., 5., operator/);
or similar does not work.
>
Thanks a lot,
Klaus
>
--
Please insert my name and a dot before my email address.
double res = opIf(10., 5., operator/);
operator / is not a global operator..
used std::divides<doubleinstead.

Closed Thread