I'm trying to execute a function that receives a parameter, which is a reference to an abstract class, through std::async
but it seems that that is not valid for some reason. On the other hand, if I replace the mentioned reference by a pointer everything works.
Why does this happen? Is it generally better to pass abstract class parameters as pointers?
See the examples below:
Incorrect use of std::async
#include <iostream>
#include <future>
class AbsClass {
public:
virtual int f() = 0;
};
class ImplClass : public AbsClass {
public:
int f() override { return 21; }
};
int func(AbsClass &asbclass) {
return 210 + asbclass.f();
}
int main() {
ImplClass ic;
AbsClass &ac = ic;
// This causes a compilation failure:
std::future<int> res = std::async(&func, ac);
std::cout << res.get() << std::endl;
}
Failure displayed
/usr/include/c++/7/future:1745:5: error: invalid abstract parameter type ‘AbsClass’
main.cpp:4:7: note: because the following virtual functions are pure within ‘AbsClass’:
class AbsClass {
^~~~~~~~
main.cpp:6:17: note: virtual int AbsClass::f()
virtual int f() = 0;
Correct use of std::async
#include <iostream>
#include <future>
class AbsClass {
public:
virtual int f() = 0;
};
class ImplClass : public AbsClass {
public:
int f() override { return 21; }
};
int func(AbsClass *asbclass) {
return 210 + asbclass->f();
}
int main() {
ImplClass ic;
AbsClass &ac = ic;
std::future<int> res = std::async(&func, &ac);
std::cout << res.get() << std::endl;
}