Answer the question
In order to leave comments, you need to log in
How to use templates when creating c++11 std::thread threads?
There is a code:
#include <iostream>
#include <algorithm>
#include <thread>
template<typename T>
T func(T& tid) {
std::cout << "Thread says: " << tid << std::endl;
}
int main()
{
std::thread t(func,35);
t.join();
return 0;
}
main.cpp:17: ошибка: no matching function for call to 'std::thread::thread(<unresolved overloaded function type>, int)'
std::thread t(func,35);
^
Answer the question
In order to leave comments, you need to log in
Help the compiler determine the function type to instantiate
#include <iostream>
#include <algorithm>
#include <thread>
template<typename T>
T func (T& tid) {
std::cout << "Thread says: " << tid << std::endl;
return tid;
}
int main ()
{
unsigned val = 35;
std::thread t (func<decltype(val)>, val);
t.join ();
system ("pause");
return 0;
}
this is how #include
works
#include
using namespace std;
template void func(T& id)
{
std::cout << "Thread cout : " << id << std::endl;
}
int main()
{
std::thread t (std::bind(&func,32));
t.join();
return 0;
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question