初学 c++的 std::future,请大佬解惑....

2019-04-02 22:39:11 +08:00
 Iain

std::future 可以这样直接定义吗?

#include <iostream></iostream>

#include <future></future>

int main() {

std::future<int> feature;

return 0;

}

2696 次点击
所在节点    C
10 条回复
wutiantong
2019-04-03 10:52:23 +08:00
可以

Default constructor. Constructs a std::future with no shared state. After construction, valid() == false.

https://en.cppreference.com/w/cpp/thread/future/future
Iain
2019-04-03 12:41:40 +08:00
@wutiantong 可是为什么我的编译器会报错,
aggregate 'std::future<int> feature' has incomplete type and cannot be defined
用其他电脑测试无错...也设置了 g++11 支持 可以提供一下解决思路吗 ?
wutiantong
2019-04-03 13:36:45 +08:00
@Iain
1. 检查你的编译器是否支持 c++11 或以上
2. 检查你编译时是否打开了 c++11 或以上的开关 ( -std=c++11 )
3. 确认代码里加了头文件 #include <future>
Iain
2019-04-03 13:57:21 +08:00
@wutiantong
我好像发现问题所在了,请问 mingw 的 thread model (有 posix、和 win32 ) 这个是否影响 c++11 新特性呢?
mingw 4.9.2 posix 成功编译
mingw 6.3.0 win32 编译失败
wutiantong
2019-04-03 14:50:09 +08:00
@Iain 是这个问题,看这里就知道了: https://stackoverflow.com/a/30390278/6947776
Iain
2019-04-03 18:37:05 +08:00
@wutiantong 您好,十分感谢您百忙之中为我解惑。
我又遇到一个问题...
mingw-w64 两种 thread model ( posix /win32 ) 通过 makefile 生成的 dll 文件为何相差很大,一个 148kb ( win32 )一个 501kb ( posix ),而且通过 System.load 调用只能识别 win32 生成的 dll,对于 posix 模式生成的 dll 显示未找到
wutiantong
2019-04-04 09:49:35 +08:00
@Iain 看起来这个项目应该能帮到你: https://github.com/meganz/mingw-std-threads
Iain
2019-04-04 10:21:28 +08:00
@wutiantong 感谢你的回复
我分别使用 mingw-w64 8.1.0、6.3.0、5.4.0、4.8.1 都是 posix thread model ;只有 5.4.0 版本生成的 dll 可以识别。
wutiantong
2019-04-04 10:46:16 +08:00
@Iain 找不到 dll 的问题多半还是跟你的编译环境和流程有关,但我没接触过 mingw 这方面的开发,爱莫能助啦
Iain
2019-04-04 13:42:55 +08:00
@wutiantong
问题目前暂时是解决了,很感谢您的帮助!

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/551446

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX