libc++的一个错误?未来和C++11

A bug for libc++ ? future and C++11

本文关键字:错误 未来 C++11 一个 libc++      更新时间:2023-10-16
#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.1libstdc++,它无法在与clanglibc++相同的平台上编译,因为libc++库显然没有以正确的方式实现所有内容。

这是 clang 的 c++ 标准库的错误还是我的代码有问题?

我正在使用官方 llvm apt 存储库中libc++libc++abi1.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.

libc++ 的这个修订版修复了这个问题: http://llvm.org/viewvc/llvm-project?view=revision&revision=188413