Template function will not compile when called as a stream

I have a problem with template functions and streams:

template <class TYPE_size> void Threader(TYPE_size counter) { counter++; } int main() { unsigned int counter = 100; thread one(Threader,counter); one.join(); cout << counter; } 

It does not compile; I get:

error: there is no corresponding function to call âstd :: thread :: thread (, unsigned INT &) but

If I delete the template that it compiles, and if I change the function call to a standard function call, not a stream (still using the template), it compiles.

Does anyone know why this is?

I am using Centos5 64 bit.

  error: no matching function for call to âstd::thread::thread(<unresolved overloaded function type>, unsigned int&)â /usr/lib/gcc/x86_64-redhat-linux6E/4.4.0/../../../../include/c++/4.4.0/thread:124: note: candidates are: std::thread::thread(std::thread&&) /usr/lib/gcc/x86_64-redhat-linux6E/4.4.0/../../../../include/c++/4.4.0/thread:122: note: std::thread::thread(const std::thread&) /usr/lib/gcc/x86_64-redhat-linux6E/4.4.0/../../../../include/c++/4.4.0/thread:121: note: std::thread::thread() 
+4
source share
4 answers

There is no function named Threader. When you write Threader<int> or something else, the compiler creates a function. If you then write Threader<float> , then the compiler will create a new function. You can either specify a default template parameter, or give it a parameter when you call it.

 template <class TYPE_size=int> 

or

 thread one(Threader<int>, counter); 
+5
source

I take the liberty of proposing many corrections in order to achieve what, in my opinion, is behavior:

 #include <thread> template <typename T> void Threader(T & counter) // take by reference! { counter++; } int main() { unsigned int counter = 100; std::thread one(Threader<unsigned int>, // specify template *instance* std::ref(counter) ); // pass variable as reference one.join(); return counter; } 
+5
source

Missing argument list for your template.

Try:

  unsigned int counter = 100; thread one(Threader<unsigned int>,counter); 

or, if you are using the C ++ x0 / C ++ 11 standard, give your template a standard type, for example:

 template <typename TYPE_size = unsigned int> void Threader(TYPE_size counter) { counter++; } int main() { unsigned int counter = 100; thread one(Threader<>,counter); one.join(); cout << counter; } 
+4
source

Lambdas was introduced in C ++ 11, this can also be used in this case.

Basically, a thread is created using a lambda, where the lambda calls a function, which then allows the conclusion of a template type.

 thread one([counter]() { Threader(counter); }); 

Above, the counter is fixed by value, but, as follows from some answer, you can use capture by reference

 #include <iostream> #include <thread> template <class T> void Threader(T& counter) { counter++; } int main() { unsigned int counter = 100; std::thread one([&counter]() { Threader(counter); }); one.join(); std::cout << counter; } 

Note: this question has been marked as duplicate, therefore adding using new language features.

0
source

Source: https://habr.com/ru/post/1384123/


All Articles