Template function will not compile when called as a thread

I have a problem relating to template functions and threads:

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

This does not compile; I get:

error: no matching function for call to âstd::thread::thread(, unsigned int&)â

If I remove the template it compiles and if I change the function call to a standard function call rather than a thread (still using the template) it compiles.

Does anyone know why this is?

I'm 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()

Answers


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

template <class TYPE_size=int>

or

thread one(Threader<int>, counter);

Your missing the argument list of your template.

Try:

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

or, if you are using c++x0/c++11 standard, give your template a standard type like:

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;
}

I'm taking the liberty of offering a variety of fixes to achieve what I believe is intended behaviour:

#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;
}

Need Your Help

how to create jar using maven pom.xml

maven jar pom.xml

I need an example pom file to create a jar file for my spring project. how to specify to create a directory and folder for resources while creating jar file. I can able to create jar file using the...

How to count string num with limit memory?

java algorithm

The task is to count the num of words from a input file.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.