Yes, gcc should reject this code.
std::async
copies the arguments and forwards them as rvalues. You cannot bind an rvalue to an lvalue reference, so this fails. If you want to pass by reference usestd::ref(foo)
. In this specific example, the call tostd::async(bar,foo)
essentially does the following:template<typename F> future<foo_t*> async(F& func,foo_t& arg){ F func_copy(func); foo_t arg_copy(arg); // on background thread internal_set_future_result(func_copy(std::move(arg_copy))); return ...; }
If you use
std::ref(foo)
then the assertion should not fail. If the code doesn't compile then it's a moot point.
Odd behavior with std::async
题
Consider the following sample code:
#include <future>
#include <array>
#include <cassert>
typedef std::array<int, 5> foo_t;
foo_t* bar(foo_t& foo) {
return &foo;
}
int main() {
foo_t foo;
auto a = std::async(bar, foo);
auto b = std::async(bar, foo);
assert(a.get() == b.get());
return 0;
}
GCC 4.6.3 compiles this with no complaints. However, this fails at runtime with:
test: test.cpp:15: int main(): Assertion `a.get() == b.get()' failed.
Aborted (core dumped)
GCC 4.8.2, however, refuses to compile the file:
In file included from /usr/local/include/c++/4.8.2/future:38:0,
from test.cpp:1:
/usr/local/include/c++/4.8.2/functional: In instantiation of 'struct std::_Bind_simple<std::array<int, 5ul>* (*(std::array<int, 5ul>))(std::array<int, 5ul>&)>':
/usr/local/include/c++/4.8.2/future:1525:70: required from 'std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> std::async(std::launch, _Fn&&, _Args&& ...) [with _Fn = std::array<int, 5ul>* (&)(std::array<int, 5ul>&); _Args = {std::array<int, 5ul>&}; typename std::result_of<_Functor(_ArgTypes ...)>::type = std::array<int, 5ul>*]'
/usr/local/include/c++/4.8.2/future:1541:36: required from 'std::future<typename std::result_of<_Functor(_ArgTypes ...)>::type> std::async(_Fn&&, _Args&& ...) [with _Fn = std::array<int, 5ul>* (&)(std::array<int, 5ul>&); _Args = {std::array<int, 5ul>&}; typename std::result_of<_Functor(_ArgTypes ...)>::type = std::array<int, 5ul>*]'
test.cpp:13:30: required from here
/usr/local/include/c++/4.8.2/functional:1697:61: error: no type named 'type' in 'class std::result_of<std::array<int, 5ul>* (*(std::array<int, 5ul>))(std::array<int, 5ul>&)>'
typedef typename result_of<_Callable(_Args...)>::type result_type;
^
/usr/local/include/c++/4.8.2/functional:1727:9: error: no type named 'type' in 'class std::result_of<std::array<int, 5ul>* (*(std::array<int, 5ul>))(std::array<int, 5ul>&)>'
_M_invoke(_Index_tuple<_Indices...>)
^
This appears to be a libstdc++ issue.
So my questions are: 1 - should GCC reject this code, or is there something in the standard that I am not aware of. 2 - should the assertion fail? The expected behavior is that async functions that take the same reference should refer to the same object, but it appears that a copy is made local to the async task.
I've tried compiling with clang, but it has the same compile error issues (as it shares the same libstdc++) with 4.8.2, and it can't compile the 4.6.3 library headers.
解决方案
不隶属于 StackOverflow