STD :: Promise Store const类型如何在MSVC,GCC和Clang中类型?
最近,我已经开始使用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;
}
- Successful compilation with Clang 11: https://godbolt.org/z/o6Ge85rxG
- Successful compilation with GCC 12: https://godbolt.org/z/x1rhqhfGT
- Error with MSVC 19: https://godbolt.org/z/4jMYhTj3n
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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看起来像是一个MSVC问题,在这里报告:
https://develovelocercommunity.visualstudio.com/t/std:: promise-works-roctly-for-for-const/10092031
并链接到Github上的问题:
https://github.com/microsoft/stl/issues/2599
Looks like an MSVC issue, reported here:
https://developercommunity.visualstudio.com/t/std::promise-works-incorrectly-for-const/10092031
and linked to the issue on GitHub:
https://github.com/microsoft/STL/issues/2599