Le constructeur de copie est appelé plusieurs fois lors de la construction d’un thread par object de fonction

J’essaie de passer un object de fonction à un thread. Je suis confus quand j’ai trouvé que le constructeur de copie est appelé deux fois dans le fil principal. Pourquoi ne pas simplement copier une fois au lieu de deux fois? La deuxième copie est inutile.

Extrait de code:

#include  #include  using namespace std; struct A { A() { cout << "constructor this=" << this << " thread_id=" << this_thread::get_id() << endl; } A(const A &other) { cout << "Copy constructor this=" << this << " thread_id=" << this_thread::get_id() << endl; } void operator()() { cout << "operator() this=" << this << " thread_id=" << this_thread::get_id() << endl; } ~A() { cout << "destructor this=" << this << " thread_id=" << this_thread::get_id() << endl; } }; int main() { A a; thread t(a); t.join(); return 0; } 

Le résultat est ci-dessous:

 constructor this=0x7ffee91f5888 thread_id=0x7fff8b2a6340 Copy constructor this=0x7ffee91f5370 thread_id=0x7fff8b2a6340 Copy constructor this=0x7fb8de402870 thread_id=0x7fff8b2a6340 destructor this=0x7ffee91f5370 thread_id=0x7fff8b2a6340 operator() this=0x7fb8de402870 thread_id=0x700007c95000 destructor this=0x7fb8de402870 thread_id=0x700007c95000 destructor this=0x7ffee91f5888 thread_id=0x7fff8b2a6340