【发布时间】:2014-02-25 19:52:06
【问题描述】:
#include <thread>
#include <future>
#include <iostream>
#include <atomic>
#include <cstdint>
template <typename T>
void consumeFuture(std::future<T>&& fut) {
fut.get();
}
template <typename T>
void actOnT(std::atomic<T>& at) {
++at;
}
int main() {
std::atomic<uint32_t> a{42};
consumeFuture(std::async(std::launch::async, &actOnT<uint32_t>, std::ref(a)));
std::cout << a << "\n";
}
此代码在 Ubuntu 13.10 64 位下使用 g++ 4.8.1 和 libstdc++ 编译得很好,但由于 libc++ 库显然没有以正确的方式实施一切。
这是clang的c++标准库的bug还是我的代码有问题?
我正在使用来自官方 llvm apt 存储库的 libc++ 和 libc++abi 的 1.0~svn181765-1 版本。
使用clang++ 和libc++ 获得的输出:
In file included from future_1.cpp:1:
In file included from /usr/bin/../include/c++/v1/thread:90:
In file included from /usr/bin/../include/c++/v1/__functional_base:15:
/usr/bin/../include/c++/v1/type_traits:2761:19: error: invalid application of 'sizeof' to an incomplete type 'void'
static_assert(sizeof(_Tp) > 0, "Type must be complete.");
^~~~~~~~~~~
/usr/bin/../include/c++/v1/type_traits:2778:15: note: in instantiation of template class
'std::__1::__check_complete<void>' requested here
: private __check_complete<_Rp>
^
/usr/bin/../include/c++/v1/type_traits:2947:15: note: in instantiation of template class
'std::__1::__check_complete<void (*)(std::__1::atomic<unsigned int> &)>' requested here
: private __check_complete<_Fp>
^
/usr/bin/../include/c++/v1/type_traits:2958:11: note: in instantiation of template class
'std::__1::__invokable_imp<void (*)(std::__1::atomic<unsigned int> &),
std::__1::reference_wrapper<std::__1::atomic<unsigned int> > >' requested here
__invokable_imp<_Fp, _Args...>::value>
^
/usr/bin/../include/c++/v1/type_traits:2977:30: note: in instantiation of template class 'std::__1::__invokable<void
(*)(std::__1::atomic<unsigned int> &), std::__1::reference_wrapper<std::__1::atomic<unsigned int> > >' requested
here
: public __invoke_of_imp<__invokable<_Fp, _Args...>::value, _Fp, _Args...>
^
/usr/bin/../include/c++/v1/future:2237:17: note: in instantiation of template class 'std::__1::__invoke_of<void
(*)(std::__1::atomic<unsigned int> &), std::__1::reference_wrapper<std::__1::atomic<unsigned int> > >' requested
here
future<typename __invoke_of<typename decay<_Fp>::type, typename decay<_Args>::type...>::type>
^
future_1.cpp:19:17: note: while substituting deduced template arguments into function template 'async' [with _Fp = void
(*)(std::__1::atomic<unsigned int> &), _Args = <std::__1::reference_wrapper<std::__1::atomic<unsigned int> >>]
consumeFuture(std::async(std::launch::async, &actOnT<uint32_t>, std::ref(a)));
^
1 error generated.
【问题讨论】:
-
适用于更新版本的 clang/libc++。
-
@MarcGlisse 您能否指定您正在使用的库版本、clang 和平台?
-
由 r188413 修复到 libc++。
标签: c++ c++11 g++ clang libc++