I am trying to write my own delegate system as a substitute for boost :: functions, since the latter makes a lot of the heap allocations that I have profiled to be problematic.
I wrote this as a replacement (a simplified, actual thing uses combined memory and a new place, but it's simple enough to reproduce the error):
template<class A, class B> struct DelegateFunctor : public MyFunctor { DelegateFunctor(void (*fptr)(A, B), A arg1, B arg2) : fp(fptr), a1(arg1), a2(arg2) {} virtual void operator()() { fp(a1, a2); } void (*fp)(A, B);
and this helper function:
template<class A, class B> MyFunctor* makeFunctor(void (*f)(A,B), A arg1, B arg2) { return new DelegateFunctor<A,B>(f, arg1, arg2); }
It’s strange here:
void bar1(int a, int b) { // do something } void bar2(int& a, const int& b) { // do domething } int main() { int a = 0; int b = 1; // A: Desired syntax and compiles. MyFunctor* df1 = makeFunctor(&bar1, 1, 2); // B: Desired syntax but does not compile: MyFunctor* df2 = makeFunctor(&bar2, a, b); // C: Not even this: MyFunctor* df3 = makeFunctor(&bar2, (int&)a, (const int&)b); // D: Compiles but I have to specify the whole damn thing: MyFunctor* df4 = makeFunctor<int&, const int&>(&bar2, a, b); }
Compiler error for version C (B is similar):
error: no matching function for call to 'makeFunctor(void (*)(int&, const int&), int&, const int&)'
which is strange, because the compiler actually correctly inferred types in its error message.
Is there a way to get compilation of version B? How to boost :: bind get around this limitation?
I am using GCC 4.2.1. There are no solutions in C ++ 11.