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