standard C operator as parameter to template function

K

Klaus Schneider

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
 
O

Obnoxious User

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::endl;
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;
}
 
K

Kai-Uwe Bux

Klaus said:
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
 
L

lei.bobby

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

double res = opIf(10., 5., operator/);
operator / is not a global operator..
used std::divides<double> instead.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top