mercredi 4 octobre 2017

Reference to abstract class can't be passed to thread function?

I want to use a reference to an abstract class (A) as parameter type in an function which is invoked using std::thread. It seems not possible, because the compiler tries for some reason to compile: std::tuple<A>, even that in my code only a reference-type of A is used as parameter (never as value-type).

#include <cstdlib>
#include <thread>

class A {
public:
  virtual void a() = 0;
};

class B : public A {
public:
  virtual void a() {
  }
};

class C {
public:
  C(A& aRef) {
    thread = std::thread(&C::doSomething, this, aRef);
  }
private:
  void doSomething(A& aRef) {

  }
  std::thread thread;
};

int main(int argc, char* argv[]) {
  B b;
  C c(b);
  return EXIT_SUCCESS;
}

Will output on Visual Studio 2017:

error C2259: 'A': cannot instantiate abstract class
tuple(278): note: see reference to class template instantiation 'std::tuple<A>' being compiled
tuple(278): note: see reference to class template instantiation 'std::tuple<C *,A>' being compiled
thread(49): note: see reference to class template instantiation 'std::tuple<void (__thiscall C::* )(A &),C *,A>' being compiled
main.cpp(18): note: see reference to function template instantiation 'std::thread::thread<void(__thiscall C::* )(A &),C*const ,A&,void>(_Fn &&,C *const &&,A &)' being compiled

Why std::thread tries to compile std::tuple<A>? If I call C::doSomething directly from the main-thread, the code compiles fine.

Is there anything I'm missing here?

Aucun commentaire:

Enregistrer un commentaire