I need to pass a function to an operator. Any unary function having correct arg type. Return type can be anything. Because this is library code, I can not wrap it or cast f
to specific overload (outside of operator*
). Function takes operator*
1st arg as it own argument. Artificial example below compiles and returns correct results. But it has hardcoded int
return type—to make this example compile.
#include <tuple>
#include <iostream>
using namespace std;
template<typename T>
int operator* (T x, int& (*f)(T&) ) {
return (*f)(x);
};
int main() {
tuple<int,int> tpl(42,43);
cout << tpl * get<0>;
}
Is it possible to make operator*
to accept f
with arbitrary return type?
UPDATE - GCC bug? Code:
#include <tuple>
template<typename T, typename U>
U operator* (T x, U& (*f)(T&) ) {
return (*f)(x);
};
int main() {
std::tuple<int,int> tpl(42,43);
return tpl * std::get<0,int,int>;
}
Compiles and runs correctly with gcc462 and 453 but is reject with gcc471 and 480. So it is possible GCC regression bug. I've submitted bug report: http://gcc.gnu.org/bugzilla/show_bug.cgi?id=54111
EDIT I've changed example to use tuple as arg - it was possible trivially deduce return type in previous example.
EDIT2
Many people could not understand what is needed, so I've changed call
function to operator*
to make example more real.