gpt4 book ai didi

c++ - libc++ 的错误? future 和 C++11

转载 作者:行者123 更新时间:2023-11-30 02:49:10 24 4
gpt4 key购买 nike

#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";
}

此代码在带有 g++ 4.8.1 的 Ubuntu 13.10 64 位下编译得很好和 libstdc++ , 它无法在与 clang 相同的平台上编译和 libc++因为 libc++显然没有以正确的方式实现所有内容的库。

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

我正在使用 1.0~svn181765-1两者的版本 libc++libc++abi来自官方 llvm apt 存储库。

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.

最佳答案

关于c++ - libc++ 的错误? future 和 C++11,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21508533/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com