Marian
Marian

Reputation: 325

Passing the method of an abstract class as std::function

I have some code like this (This is just a snippet, not completely valid code):

class AbstractClass {
public:
    AbstractClass() {}
    virtual ~AbstractClass() {}
    virtual void doA() { std::cout << "doA1\n"; };
    virtual void doB() = 0;
};

class ImplClass : public AbstractClass {
public:
    ImplClass() {}
    virtual ~ImplClass() {}
    virtual void doA() override { std::cout << "doA2\n"; };
    virtual void doB() override { std::cout << "doB2\n"; };
};

Now i would like to store a call to a member method of the AbstractClass within a std::function like this:

int main() {
    AbstractClass* aClass = new ImplClass();
    std::function<void()> func = std::bind(&AbstractClass::doA, *aClass)

    delete aClass;
    return 0;
}

However this does not work, according to the compiler due to the existance of pure virtual methods within AbstractClass.

Edit: For reference the complete error

In file included from source_file.cpp:2:0:
/usr/include/c++/5/functional: In instantiation of ‘struct std::_Bind_helper<false, void (AbstractClass::*)(), AbstractClass&>’:
/usr/include/c++/5/functional:1462:5:   required by substitution of ‘template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...) [with _Func = void (AbstractClass::*)(); _BoundArgs = {AbstractClass&}]’
source_file.cpp:26:72:   required from here
/usr/include/c++/5/functional:1445:71: error: invalid abstract parameter type ‘AbstractClass’
       typedef _Bind<__func_type(typename decay<_BoundArgs>::type...)> type;
                                                                       ^
source_file.cpp:5:7: note:   because the following virtual functions are pure within ‘AbstractClass’:
 class AbstractClass {
       ^
source_file.cpp:10:18: note:    virtual void AbstractClass::doB()
     virtual void doB() = 0;
                  ^
source_file.cpp: In function ‘int main()’:
source_file.cpp:26:72: error: no matching function for call to ‘bind(void (AbstractClass::*)(), AbstractClass&)’
     std::function<void()> func = std::bind(&AbstractClass::doA, *aClass)
                                                                        ^
In file included from source_file.cpp:2:0:
/usr/include/c++/5/functional:1462:5: note: candidate: template<class _Func, class ... _BoundArgs> typename std::_Bind_helper<std::__is_socketlike<_Func>::value, _Func, _BoundArgs ...>::type std::bind(_Func&&, _BoundArgs&& ...)
     bind(_Func&& __f, _BoundArgs&&... __args)
     ^
/usr/include/c++/5/functional:1462:5: note:   substitution of deduced template arguments resulted in errors seen above
/usr/include/c++/5/functional:1490:5: note: candidate: template<class _Result, class _Func, class ... _BoundArgs> typename std::_Bindres_helper<_Result, _Func, _BoundArgs>::type std::bind(_Func&&, _BoundArgs&& ...)
     bind(_Func&& __f, _BoundArgs&&... __args)
     ^
/usr/include/c++/5/functional:1490:5: note:   template argument deduction/substitution failed:
source_file.cpp:26:72: note:   couldn't deduce template parameter ‘_Result’
     std::function<void()> func = std::bind(&AbstractClass::doA, *aClass)
                                                                        ^

Is there any solution to my problem? I haven't found one so far.

Upvotes: 2

Views: 1985

Answers (2)

Richard Hodges
Richard Hodges

Reputation: 69902

The current best advice is to favour lambdas over std::bind, which is seen as something of an anachronism in the standard since lambdas arrived.

Lambdas:

  1. are more explicit
  2. are more efficient
  3. are less confusing
  4. require no convoluted constructs like std::ref and std::cref

example:

#include <functional>
#include <iostream>

class AbstractClass {
public:
    AbstractClass() {}
    virtual ~AbstractClass() {}
    virtual void doA() { std::cout << "doA1\n"; };
    virtual void doB() = 0;
};

class ImplClass : public AbstractClass {
public:
    ImplClass() {}
    virtual ~ImplClass() {}
    virtual void doA() override { std::cout << "doA2\n"; };
    virtual void doB() override { std::cout << "doB2\n"; };
};

int main() {
    AbstractClass* aClass = new ImplClass();

    // aClass is a pointer, and a copy of that pointer will be
    // captured. 
    std::function<void()> func = [aClass]{ aClass->doA(); };

    func();

    delete aClass;
    return 0;
}

Upvotes: 2

You code binds the object pointed to by aClass by value. You want to bind it by reference:

std::function<void()> func = std::bind(&AbstractClass::doA, std::ref(*aClass));

Or just bind aClass directly, pointers can be used for implicit this too:

std::function<void()> func = std::bind(&AbstractClass::doA, aClass);

[Live example]

Upvotes: 11

Related Questions