简体   繁体   English

如何修复错误&#39;std :: promise <int> :: promise(const std :: promise <int> &)&#39;:尝试引用已删除的函数

[英]How to fix error 'std::promise<int>::promise(const std::promise<int> &)' : attempting to reference a deleted function

I am trying to understand 'future', 'promise' and 'async'. 我试图理解“未来”,“承诺”和“异步”。 So I copied the following code nearly verbatim from cppreference.com . 所以我从cppreference.com几乎逐字复制了以下代码。 The compiler gives the error std::promise<int>::promise(const std::promise<int> &) : attempting to reference a deleted function. 编译器给出错误std::promise<int>::promise(const std::promise<int> &) :尝试引用已删除的函数。

#include "stdafx.h"
#include <iostream>
#include <future>
#include <thread>

int main()
{
  // future from a packaged_task
  std::packaged_task<int()> task([](){ return 7; }); // wrap the function
  std::future<int> f1 = task.get_future();  // get a future
  std::thread(std::move(task)).detach(); // launch on a thread

// future from an async()
  std::future<int> f2 = std::async(std::launch::async, [](){ return 8; });

// future from a promise
  std::promise<int> p;
  std::future<int> f3 = p.get_future();
  std::thread( [](std::promise<int> p){ p.set_value_at_thread_exit(9); }, //<-- error occurs here
             std::move(p) ).detach();

  std::cout << "Waiting..." << std::flush;
  f1.wait();
  f2.wait();
  f3.wait();
  std::cout << "Done!\nResults are: "
            << f1.get() << ' ' << f2.get() << ' ' << f3.get() << '\n';
}

I see why, the 'promise' p can't be copied due to the copy constructor. 我知道为什么,由于复制构造函数而无法复制'promise'p。 But since it is not my class, I can't change it. 但是由于这不是我的课程,所以我无法更改。 So my questions are: 所以我的问题是:

1) This is an example from a C++ reference website, does it compile on any other compiler? 1)这是来自C ++参考网站的示例,它可以在任何其他编译器上编译吗? (I am using VS 2013). (我正在使用VS 2013)。

2) What can I do to fix the error and get the example to work as intended? 2)我该如何解决该错误并使该示例按预期工作?

NOTE: I answered my own question below. 注意:我在下面回答了我自己的问题。 But now the example program fails a debug assertion at ... 但是现在示例程序在...上的调试断言失败了

f3.wait();

So, though my change allows it to compile, now I have a runtime error. 因此,尽管我的更改允许它进行编译,但是现在我遇到了运行时错误。 Is that due to my change? 那是我的改变吗? Or is it another bug? 还是另一个错误? The error is "Debug Error! ... R6010 - abort() has been called". 错误为“调试错误!... R6010-已调用abort()”。

NOTE: I modified my answer below. 注意:我在下面修改了答案。 I decided to use a pointer instead of a reference. 我决定使用指针而不是引用。

Sorry, I just figured it out. 抱歉,我只是想通了。 I changed the function to pass a pointer as follows... 我将函数更改为如下所示传递指针...

std::thread( [](std::promise<int>* p){ p->set_value_at_thread_exit(9); }, //<-- pointer fixes error
         &p).detach();

I had tried using a reference. 我曾尝试使用参考。 That fixed the original error but then I ran into a failed debug assertion "Debug Error! ... R6010 - abort() has been called". 修复了原始错误,但是随后我遇到了一个失败的调试断言“调试错误!... R6010-已调用abort()”。 While debugging, I found the promise destructor being called after the promise task completed but before F3.wait() was called. 在调试时,我发现在Promise任务完成之后但在F3.wait()被调用之前,将调用Promise析构函数。

So, that answers question #2. 因此,这回答了问题2。 I assume the answer to question #1 is 'no'. 我认为问题1的答案是“否”。 I will let the sight know about the bug (will edit it if I can). 我会让视线了解该错误(如果可以的话,请对其进行编辑)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 错误C2280:&#39;std :: thread :: thread(const std :: thread&)&#39;:尝试引用已删除的函数 - Error C2280: 'std::thread::thread(const std::thread &)' : attempting to reference a deleted function C ++线程和承诺:尝试引用已删除的函数 - C++ Thread and Promise : attempting to reference a deleted function std::promise 如何在 MSVC、GCC 和 Clang 中存储 const 类型? - How does std::promise store const types in MSVC, GCC, and Clang? std ::带有临时std :: promise的未来 - std::future with temporary std::promise std::promise 和 std::future 的生命周期 - Lifetime of std::promise and std::future std :: async vs std :: promise - std::async vs std::promise 未定义引用`cv :: error(int,std :: string const&,char const *,char const *,int)&#39; - undefined reference to `cv::error(int, std::string const&, char const*, char const*, int)' 尝试使用std :: bind重新分配std :: function并得到错误“试图引用已删除的函数” - attempting to reassign std::function with std::bind and getting error “attempting to reference a deleted function” 什么是 std::promise? - What is std::promise? 错误:对`cv::imread(std::string const&amp;, int)&#39;的未定义引用 - error: undefined reference to `cv::imread(std::string const&, int)'
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM