lundi 9 mars 2015

c++: error: no type named ‘type’ in ‘class std::result_of

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



#include <iostream>
#include <unordered_map>
#include <thread>

using namespace std;

void thread_add(unordered_map<int, int>& ht, int from, int to)
{
for(int i = from; i <= to; ++i)
ht.insert(unordered_map<int, int>::value_type(i, 0));
}

void test()
{
unordered_map<int, int> ht;
thread t[2];

t[0] = thread(thread_add, ht, 0, 9);
t[1] = thread(thread_add, ht, 10, 19);

t[0].join();
t[1].join();

std::cout << "size: " << ht.size() << std::endl;
}

int main()
{
test();
return 0;
}


However, there are errors when compiling it.



$ g++ -std=c++11 -pthread test.cpp
...
/usr/include/c++/4.8.2/functional:1697:61: error: no type named ‘type’ in ‘class std::result_of<void (*(std::unordered_map<int, int>, int, int))(std::unordered_map<int, int>&, int, int)>’
typedef typename result_of<_Callable(_Args...)>::type result_type;
...


Took a while but still cannot correct it. Thanks.


Aucun commentaire:

Enregistrer un commentaire