[Bug c++/68784] New: deductible parameter type still requires explicit reference cast, e.g., std::thread
xuancong84 at gmail dot com
gcc-bugzilla@gcc.gnu.org
Tue Dec 8 08:20:00 GMT 2015
https://gcc.gnu.org/bugzilla/show_bug.cgi?id=68784
Bug ID: 68784
Summary: deductible parameter type still requires explicit
reference cast, e.g., std::thread
Product: gcc
Version: 4.8.4
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: c++
Assignee: unassigned at gcc dot gnu.org
Reporter: xuancong84 at gmail dot com
Target Milestone: ---
G++ compiler is weaker than MSVC on this point.
The following piece of code is compilable using MSVC but not in g++
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 main()
{
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;
}
error: no type named ‘type’ in ‘class std::result_of ...
g++ require explicit reference cast:
t[0] = thread(thread_add, ref(ht), 0, 9);
However, this is unnecessary because it can be easily deduced from the
definition of the thread_add function whether to pass in a newly created copy
or a reference to the existing copy.
I hope the compiler can be improved to resolve all these deducible parameter
types so that programmers do not need to explicitly write the reference cast
std::ref() every time, like what has been done in MSVC.
More information about the Gcc-bugs
mailing list