Please consider this code, which I obtained from another of my questions:
#include <thread>
using namespace std;
class CObject {};
void MyThreadFunc(CObject&) {}
void MyThreadFunc(CObject&&){}
template<typename FunctionType, typename ...Args>
void StartDetachedThread(FunctionType func, Args&&... args)
{
thread([&]()
{
func(forward<Args>(args)...);
}).detach();
}
int main()
{
CObject object;
StartDetachedThread<void(CObject&)>(MyThreadFunc, std::ref(object));
CObject object2;
StartDetachedThread<void(CObject&&)>(MyThreadFunc, std::move(object2));
return 0;
}
The calls to StartDetachedThread
has the template argument specified in order that if I pass an r-value reference through to it, it calls the version of MyThreadFunc
that expects an r-value reference; the same for the normal reference version.
Quite simply, I don't understand how it works when I have only specified one template argument type (the type of the function I want my thread to run) to a function that expects two - can someone please clarify?