STD :: Promise Store const类型如何在MSVC,GCC和Clang中类型?

发布于 2025-02-13 20:32:39 字数 1817 浏览 0 评论 0原文

最近,我已经开始使用MSVC编译了总是用GCC和Clang编译的代码。此代码的某些部分产生了一个有趣的汇编错误(截断):

C:/data/msvc/14.33.31424-Pre/include\future(311) error C2678: binary '=': no operator found which takes a left-hand operand of type 'const Result' (or there is no acceptable conversion)
<source>(7): note: could be 'Result &Result::operator =(Result &&)'
<source>(7): note: or       'Result &Result::operator =(const Result &)'

此代码的最小示例:

#include <future>
#include <iostream>

struct Result {
    int data{0};
};

// getting rid of constness resolves the problem for MSVC
using result_t = const Result; 
using promise_result_t = std::promise<result_t>;

auto compute_result(promise_result_t result)
{
    Result some_result{10};
    result.set_value(std::move(some_result));
}

int main()
{
    promise_result_t my_promise;
    auto my_future = my_promise.get_future();
    std::thread thread(compute_result, std::move(my_promise));
    std::cout << "result: " << my_future.get().data;
    thread.join();
    return 0;
}
  • 用clang 11的成功编译11: https://godbolt.org/z/o6ge85rxg
  • 成功用GCC 12:
  • https://godbolt.org/z/x1rhqhfgt href =“ https://godbolt.org/z/4jmyhtj3n” rel =“ noreferrer”> https://godbolt.org/z/4jmyhtj3n

很明显,清除const const 对于std :: Promise存储解决问题,但是我很好奇是否有人试图深入研究针对不同编译器的std :: Promise的实现。

允许此代码可以使用GCC和Clang进行编译和工作的区别,而在使用MSVC时会产生汇编错误? (通过指针在GCC和Clang中存储的数据操作?

Recently, I've started using MSVC to compile a code which was always compiled with GCC and Clang. Some piece of this code produces an interesting compilation error (truncated):

C:/data/msvc/14.33.31424-Pre/include\future(311) error C2678: binary '=': no operator found which takes a left-hand operand of type 'const Result' (or there is no acceptable conversion)
<source>(7): note: could be 'Result &Result::operator =(Result &&)'
<source>(7): note: or       'Result &Result::operator =(const Result &)'

Minimal example of this piece of code:

#include <future>
#include <iostream>

struct Result {
    int data{0};
};

// getting rid of constness resolves the problem for MSVC
using result_t = const Result; 
using promise_result_t = std::promise<result_t>;

auto compute_result(promise_result_t result)
{
    Result some_result{10};
    result.set_value(std::move(some_result));
}

int main()
{
    promise_result_t my_promise;
    auto my_future = my_promise.get_future();
    std::thread thread(compute_result, std::move(my_promise));
    std::cout << "result: " << my_future.get().data;
    thread.join();
    return 0;
}

It is quite clear that removal of const for std::promise storage solves the problem, but I'm curious if anyone has tried to dive deep into the implementations of std::promise for different compilers.

What might be the difference that allows this code to compile and work using GCC and Clang, yet produce a compilation error when using MSVC? (Operating via pointers on stored data in GCC and Clang?)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文