Template function will not compile when called as a thread

前端 未结 4 755
暗喜
暗喜 2021-01-04 20:22

I have a problem relating to template functions and threads:

template 
void Threader(TYPE_size counter)
{
    counter++;
}
int main()
         


        
4条回答
  •  暖寄归人
    2021-01-04 20:59

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

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

提交回复
热议问题