c++: error: no type named ‘type’ in ‘class std::result_of<void (*(std::unordered_map

前端 未结 2 672
独厮守ぢ
独厮守ぢ 2020-12-30 20:30

Following is just a simple program to test using two threads to insert a hash table. For test no lock is used.

#include 
#include 

        
2条回答
  •  既然无缘
    2020-12-30 20:58

    The error is very cryptic indeed, but the problem is that thread_add takes its first parameter by reference, but you're passing it by value. This causes the functor type to be deduced wrong. If you want to pass something actually by reference to a functor like std::bind or the main function of a std::thread, you need to use a reference wrapper (std::ref):

    void test()
    {
        // ...
    
        t[0] = thread(thread_add, std::ref(ht), 0, 9);
        t[1] = thread(thread_add, std::ref(ht), 10, 19);
    
        // ...
    }
    

    [Live example]

提交回复
热议问题