473,387 Members | 1,465 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

standard C operator as parameter to template function

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.

Jun 22 '07 #1
3 1475
On Fri, 22 Jun 2007 11:31:11 +0200, Klaus Schneider wrote:
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
Jun 22 '07 #2
Klaus Schneider wrote:
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
Jun 22 '07 #3
On 6 22 , 5 31 , Klaus Schneider <schnei...@iup.uni-heidelberg.de>
wrote:
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.

Jun 22 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Guenther Brunthaler | last post by:
Hi template specialists, I have a problem with the code listed below. What I wanted to do is defining an operator<< that is able to output a 'matrix<ELEMENT_T, INDEX_T>::subrange' object into...
9
by: Alexander Stippler | last post by:
Hi, I have got a question concerning the overload resolution rules of C++ and enumeration types. The following little test program illustrates the situation: enum { red, green, blue }; ...
3
by: Sensei | last post by:
Hi. I have a problem with a C++ code I can't resolve, or better, I can't see what the problem should be! Here's an excerpt of the incriminated code: === bspalgo.cpp // THAT'S THE BAD...
43
by: Steven T. Hatton | last post by:
Now that I have a better grasp of the scope and capabilities of the C++ Standard Library, I understand that products such as Qt actually provide much of the same functionality through their own...
13
by: Atlas | last post by:
Hi, I implemented a template as: template <int L, int M, int T> class Quantity { ..... public: friend Quantity operator*(const Quantity& q1,const Quantity& q2); ..... };
13
by: Dan Tsafrir | last post by:
is the following code standard? (cleanly compiles under g++-4.0.2): struct Asc { bool operator()(int a, int b) {return a < b;} }; struct Des { bool operator()(int a, int b) {return b > a;} };...
0
by: k04jg02 | last post by:
Python has a nifty operator that will take a container and pass its elements as function parameters. In Python you can make a list like so: x = Then you can say: f(*x)
4
by: aaragon | last post by:
Hi everyone, I was unable to find out why my code is not compiling. I have a template class and I'm trying to write the operator<< for standard output. Does anyone know why this is not right?...
9
by: puzzlecracker | last post by:
From my understanding, if you declare any sort of constructors, (excluding copy ctor), the default will not be included by default. Is this correct? class Foo{ public: Foo(int); // no...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.