V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
Iain
V2EX  ›  C

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

  •  1
     
  •   Iain · 2019-04-02 22:39:11 +08:00 · 2681 次点击
    这是一个创建于 1822 天前的主题,其中的信息可能已经有所发展或是发生改变。

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

    #include <iostream></iostream>

    #include <future></future>

    int main() {

    std::future<int> feature;
    
    return 0;
    

    }

    10 条回复    2019-04-04 13:42:55 +08:00
    wutiantong
        1
    wutiantong  
       2019-04-03 10:52:23 +08:00   ❤️ 1
    可以

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

    https://en.cppreference.com/w/cpp/thread/future/future
    Iain
        2
    Iain  
    OP
       2019-04-03 12:41:40 +08:00
    @wutiantong 可是为什么我的编译器会报错,
    aggregate 'std::future<int> feature' has incomplete type and cannot be defined
    用其他电脑测试无错...也设置了 g++11 支持 可以提供一下解决思路吗 ?
    wutiantong
        3
    wutiantong  
       2019-04-03 13:36:45 +08:00   ❤️ 1
    @Iain
    1. 检查你的编译器是否支持 c++11 或以上
    2. 检查你编译时是否打开了 c++11 或以上的开关 ( -std=c++11 )
    3. 确认代码里加了头文件 #include <future>
    Iain
        4
    Iain  
    OP
       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
        5
    wutiantong  
       2019-04-03 14:50:09 +08:00   ❤️ 1
    @Iain 是这个问题,看这里就知道了: https://stackoverflow.com/a/30390278/6947776
    Iain
        6
    Iain  
    OP
       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
        7
    wutiantong  
       2019-04-04 09:49:35 +08:00   ❤️ 1
    @Iain 看起来这个项目应该能帮到你: https://github.com/meganz/mingw-std-threads
    Iain
        8
    Iain  
    OP
       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
        9
    wutiantong  
       2019-04-04 10:46:16 +08:00   ❤️ 1
    @Iain 找不到 dll 的问题多半还是跟你的编译环境和流程有关,但我没接触过 mingw 这方面的开发,爱莫能助啦
    Iain
        10
    Iain  
    OP
       2019-04-04 13:42:55 +08:00
    @wutiantong
    问题目前暂时是解决了,很感谢您的帮助!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   我们的愿景   ·   实用小工具   ·   1048 人在线   最高记录 6543   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 28ms · UTC 19:15 · PVG 03:15 · LAX 12:15 · JFK 15:15
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.