Using infix operators as argument for a function

A

Anton

Hey all

I have a function, which I would like to get an arithmetic infix
operator (+,-,/,*) as an argument. I have reduced my problem to the
following lines of code:
#include <iostream>
using namespace std;

class MyClass{
public:
int test(int, int, int(*f)(int,int));
int add(int,int);
};
int MyClass::test(int i, int j, int (*func)(int,int)){
return func(i,j);
}
int add(int i, int j){return i+j;}
int main(){
MyClass mc;
// cout << mc.test(5,10,&add) << endl;
// cout << mc.test(5,10,&operator+) << endl;
// cout << mc.test(5,10,&(int::eek:perator+)) << endl;
return 0;
}

The first cout-line in main works.
The second gets the following compiler error:
test.cpp: In function 'int main()':
test.cpp:24: error: no matching function for call to 'MyClass::test(int, int, <unresolved overloaded function type>)'
test.cpp:12: note: candidates are: int MyClass::test(int, int, int (*)(int, int))

The third line gets another error:
test.cpp: In function 'int main()':
test.cpp:25: error: expected primary-expression before 'int'
test.cpp:25: error: expected `)' before 'int'

Is passing an infix operator as argument doable or do I need the wrap
around used in the fiorst line of main?

Anton
Computer science student
 
M

Marcel Müller

What about a functor?

#include <iostream>
using namespace std;
class MyClass{
public:
template<typename O>
int test(int, int, O f);
};
template<typename O>
int MyClass::test(int i, int j, O f){
return f(i,j);
}
int main(){
MyClass mc;
cout << mc.test(5,10,std::plus<int>()) << endl;
return 0;
}

The second gets the following compiler error:

Well, taking the address of an overloaded function is something special.

The third line gets another error:

The operator + is a free function. Otherwise the implicit conversions
were non symmetric. Furthermore int is no class.

Is passing an infix operator as argument doable or do I need the wrap
around used in the fiorst line of main?

If the functor approach is not suitable to you, you need wrappers. But
beware, calling trivial operators through a function pointer may slow
down your application by a large factor, since the lookup for the right
operator is done at runtime each time. The functor on the other side is
as fast as if you had written i+j because everything is done at compile
time.


Marcel
 
J

James Kanze

I have a function, which I would like to get an arithmetic
infix operator (+,-,/,*) as an argument. I have reduced my
problem to the following lines of code:
The first cout-line in main works.
The second gets the following compiler error:
The third line gets another error:
Is passing an infix operator as argument doable or do I need
the wrap around used in the fiorst line of main?

If the infix operator is a user defined function, it can be used
just as any function is. If it is on a built-in type, as above,
the function simply doesn't exist; you'd need to provide one.
 

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,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top