Home > database >  std::function inside template class giving object creation error
std::function inside template class giving object creation error

Time:10-03

Getting below error for the code given below. Trying to have 3 std::function inside an template class and then initialize them with different functions from different classes.

Why is this error happening? What's wrong with below code?

enter image description here

template <typename T>
class CFuntion {
public:
    std::function<void()> callback_1;
    std::function<void()> callback_2;
    std::function<void()> callback_3;

    template <typename A, typename B, typename C>
    CFuntion(A cb1, B cb2, C cb3)
        : callback_1(cb1), callback_2(cb2), callback_3(cb3)
    { }
};

class Impl1 {
public:
    void do_something1() {
        cout << "Inside Impl1 do_something1" << endl;
    }

    void do_something2() {
        cout << "Inside Impl1 do_something2" << endl;
    }
};

class Impl2 {
public:
    void do_something1() {
        cout << "Inside Impl2 do_something1" << endl;
    }

    void do_something2() {
        cout << "Inside Impl2 do_something2" << endl;
    }
};

class Impl3 {
public:
    void do_something1() {
        cout << "Inside Impl3 do_something1" << endl;
    }

    void do_something2() {
        cout << "Inside Impl3 do_something2" << endl;
    }
};

int main()
{
    unique_ptr<CFuntion<void>> ptr1 = make_unique<CFuntion<void>>(std::bind(&Impl1::do_something1, &Impl2::do_something1, 
                                        &Impl3::do_something2));
    ptr1->callback_1();
    ptr1->callback_2();
    ptr1->callback_3();

    system("pause");
    return 0;
}

CodePudding user response:

Look at this part:

unique_ptr<CFuntion<void>> ptr1 = make_unique<CFuntion<void>(std::bind(&Impl1::do_something1, &Impl2::do_something1, &Impl3::do_something2));
//                                                           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Here, you pass only one object to your CFunction<void>'s constructor. What you needed might be this:

// You need to pass temporary objects to 'std::bind()' to bind non-static member functions
Impl1 obj1;
Impl2 obj2;
Impl3 obj3;
std::unique_ptr<CFuntion<void>> ptr1 = std::make_unique<CFuntion<void>>(std::bind(&Impl1::do_something1, obj1), std::bind(&Impl2::do_something1, obj2), std::bind(&Impl3::do_something2, obj3));

Or, by looking at your Impl1, Impl2 and Impl3 classes it seems you could just make their member methods static (As none of those three classes seem to need any sort of distinction between their objects or have any instance variables to access/modify.):

class Impl1 {
public:
    static void do_something1() {
        cout << "Inside Impl1 do_something1" << endl;
    }

    static void do_something2() {
        cout << "Inside Impl1 do_something2" << endl;
    }
};

class Impl2 {
public:
    static void do_something1() {
        cout << "Inside Impl2 do_something1" << endl;
    }

    static void do_something2() {
        cout << "Inside Impl2 do_something2" << endl;
    }
};

class Impl3 {
public:
    static void do_something1() {
        cout << "Inside Impl3 do_something1" << endl;
    }

    static void do_something2() {
        cout << "Inside Impl3 do_something2" << endl;
    }
};

That way, you just need to do this, which appears to be what you want to do:

std::unique_ptr<CFuntion<void>> ptr1 = std::make_unique<CFuntion<void>>(std::bind(&Impl1::do_something1), std::bind(&Impl2::do_something1), std::bind(&Impl3::do_something2));
  • Related