+ 2
Thread class constructor
Hi I am having a doubt on how to have generic constructor that accept any function pointer. Let me try to provide more details. As we know, std::thread class can take function as argument . This means constructor of thread class has capability to accept function pointer. My query is how to have this as generic function pointer? We need to define data types for function pointer , but how it is taking care of all different signatures of function pointer for thread class constructor?
2 Answers
+ 3
It takes the function type as a template parameter (F), and the argument types as Variadic template parameters. It also checks if F is callable. Then using Variadic pack expansion you can simple pass on the arguments to the function when you are calling it. Something like this
https://code.sololearn.com/cMOhoFv0Sz70/?ref=app
Variadic templates AKA parameter pack:
https://en.cppreference.com/w/cpp/language/parameter_pack
0
Thank you