LsH
LsH

Reputation: 55

C++ Template issue with additional arg

I have a situation (on an embedded system) where I want call functions from another task. I found this method to do so http://www.drdobbs.com/elegant-function-call-wrappers/184401385 . But I what to do so with more arguments. Best case I have a solution where this is in the template to, but I have no clue how to realise that. Anyway, it is also possible to create two temples as shown below (in a small visual studio C++ test). Everything function fine, except the DeferCall2 call that give a compile time error. What mistake I made here?

#include "stdafx.h"
#include "stdlib.h"
#include "stdio.h"



class Functor
{
public:
    virtual ~Functor() {}
    virtual void operator()() = 0;
};
template< class CalleePtr, class MemFunPtr, class Parm1 >
class MemberFunctor1 : public Functor
{
public:
    MemberFunctor1
        (
        const CalleePtr & pCallee,
        const MemFunPtr & pFunction,
        Parm1 aParm1
        ) :
        pCallee(pCallee),
        pFunction(pFunction),
        aParm1(aParm1)
    {
    }

    virtual void operator()()
    {
        if ((pCallee != NULL) &&
            (pFunction != NULL))
        {
            ((*pCallee).*pFunction)(aParm1);
        }
    }

private:
    CalleePtr pCallee;
    MemFunPtr pFunction;
    Parm1 aParm1;
};

template< class CalleePtr, class MemFunPtr, class Parm1, class Parm2 >
class MemberFunctor2 : public Functor
{
public:
    MemberFunctor2
        (
        const CalleePtr & pCallee,
        const MemFunPtr & pFunction,
        Parm1 aParm1,
        Parm2 aParm2
        ) :
        pCallee(pCallee),
        pFunction(pFunction),
        aParm1(aParm1),
        aParm2(aParm2)
    {
    }

    virtual void operator()()
    {
        if ((pCallee != NULL) &&
            (pFunction != NULL))
        {
            ((*pCallee).*pFunction)(aParm1, aParm2);
        }
    }

private:
    CalleePtr pCallee;
    MemFunPtr pFunction;
    Parm1 aParm1;
    Parm2 aParm2;
};

template< class CalleePtr, class Callee, class Ret, class Type1, class Parm1 >
inline Functor * DeferCall
(
const CalleePtr & pCallee,
Ret(Callee::*pFunction)(Type1),
const Parm1 & rParm1
)
{
    return new
        MemberFunctor1< CalleePtr,
        Ret(Callee::*)(Type1), Parm1 >
        (pCallee, pFunction, rParm1);
}

template< class CalleePtr, class Callee, class Ret, class Type1, class Parm1, class Parm2>
Functor * DeferCall2
(
    const CalleePtr & pCallee,
    Ret(Callee::*pFunction)(Type1),
    const Parm1 & rParm1,
    const Parm2 & rParm2
)
{
    return new
        MemberFunctor2< CalleePtr,
        Ret(Callee::*)(Type1), Parm1, Parm2 >
        (pCallee, pFunction, rParm1, rParm2);
}

class TestObj
{
public:
    void test1 (int a)
    {
        printf("test 1 a=%d\r\n", a);
    }

    void test2 (int a, int b)
    {
        printf("test 2 a=%d, b=%d\r\n", a, b);
    }

    void test (void)
    {
        Functor * pCall = DeferCall(this, &TestObj::test1, 3);

        Functor * pCall1 = new MemberFunctor1 < TestObj *, void (TestObj::*)(int), int >(this, &TestObj::test1, 6);

        Functor * pCall2 = new MemberFunctor2 < TestObj *, void (TestObj::*)(int, int), int,  int >(this, &TestObj::test2, 4, 5);

        Functor * pCall3 = DeferCall2(this, &TestObj::test2, 3, 3); // Compile time error.

        if (pCall)
            (*pCall)();
        if (pCall1)
            (*pCall1)();
        if (pCall2)
            (*pCall2)();
        if (pCall3)
            (*pCall3)();

        delete pCall;
        delete pCall1;
        delete pCall2;
        delete pCall3;
    }
};


int _tmain(int argc, _TCHAR* argv[])
{
    TestObj *obj = new TestObj();
    obj->test();
    delete obj;

    return 0;
}

Upvotes: 0

Views: 63

Answers (2)

NetVipeC
NetVipeC

Reputation: 4432

You forgot the second int parameter in the function:

template <class CalleePtr, class Callee, class Ret, class Type1, class Parm1, class Parm2>
Functor* DeferCall2(const CalleePtr& pCallee, Ret (Callee::*pFunction)(Type1, Type1), const Parm1& rParm1,
                    const Parm2& rParm2) {
    return new MemberFunctor2<CalleePtr, Ret (Callee::*)(Type1, Type1), Parm1, Parm2>(pCallee, pFunction, rParm1, rParm2);
}

Changes are here:

Ret (Callee::*pFunction)(Type1, Type1)
                              ^^^^^^^

in the two places that appear Or add another template parameter if the second type is not always same as first.

The calling functors has two ints: void (TestObj::*)(int, int)

Upvotes: 1

Barry
Barry

Reputation: 303487

You are calling:

DeferCall2(this, &TestObj::test2, 3, 3);

where &TestObj::test2 has type void (TestObject::*)(int, int). The signature of DeferCall2 has unnecessarily many template parameters, but the second argument it takes has type:

Ret(Callee::*pFunction)(Type1)

That is, a pointer to non-cv-qualified, non-variadic member function that takes one argument. Hence the deduction failure. You're missing a second type there.


Note that you're effectively just re-implementing std::bind:

auto call3 = std::bind(this, &TestObj::test2, 3, 3);
call3();

Upvotes: 0

Related Questions