对std::future执行then会导致编译错误
Implementation of then for std::future results in compilation error
当我试图编译这个实现Visual Studio 2013 Update 3时,编译器说我引用了std::future
的已删除副本构造函数
#include <iostream>
#include <stdexcept>
#include <utility>
#include <chrono>
#include <future>
#include <thread>
template <typename T, typename Func>
auto then(std::future<T> future, Func func) -> std::future<decltype(func(future))>
{
return std::async([] (std::future<T> future, Func func) {
future.wait();
func(std::move(future));
}, std::move(future), std::move(func));
}
int main()
{
then(std::async([] {
std::this_thread::sleep_for(std::chrono::seconds(4));
return 5;
}), [] (std::future<int> f) {
std::cout << f.get();
}).get();
}
这是来自编译器的消息
1>------ Build started: Project: temp, Configuration: Debug Win32 ------
1> temp.cpp
1>c:program files (x86)microsoft visual studio 12.0vcincludexrefwrap(58): error C2280: 'std::future<std::_Result_of<_Fty,>::type>::future(const std::future<std::_Result_of<_Fty,>::type> &)' : attempting to reference a deleted function
1> with
1> [
1> _Fty=main::<lambda_671f15aca9913797c9b157283a9bc860>
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludefuture(1140) : see declaration of 'std::future<std::_Result_of<_Fty,>::type>::future'
1> with
1> [
1> _Fty=main::<lambda_671f15aca9913797c9b157283a9bc860>
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludexrefwrap(118) : see reference to class template instantiation 'std::_Result_of<_Fty,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type> ,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f> &>' being compiled
1> with
1> [
1> _Fty=then::<lambda_fce0ad2065944df99f1af1e657f83fe2>
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludefunctional(975) : see reference to class template instantiation 'std::result_of<_Funx (std::future<std::_Result_of<_Fty,>::type> ,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f> &)>' being compiled
1> with
1> [
1> _Funx=then::<lambda_fce0ad2065944df99f1af1e657f83fe2>
1> , _Fty=main::<lambda_671f15aca9913797c9b157283a9bc860>
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludexrefwrap(283) : see reference to class template instantiation 'std::_Do_call_ret<false,_Ret,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::tuple<std::future<std::_Result_of<_Fty,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>,std::tuple<>,std::_Arg_idx<0,1>>' being compiled
1> with
1> [
1> _Ret=void
1> , _Fty=main::<lambda_671f15aca9913797c9b157283a9bc860>
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludefunctional(228) : see reference to function template instantiation '_Ret std::_Callable_obj<std::_Bind<false,_Ret,_Ty,std::future<std::_Result_of<_Fty,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>,false>::_ApplyX<_Rx,>(void)' being compiled
1> with
1> [
1> _Ret=void
1> , _Ty=then::<lambda_fce0ad2065944df99f1af1e657f83fe2>
1> , _Fty=main::<lambda_671f15aca9913797c9b157283a9bc860>
1> , _Rx=void
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludefunctional(228) : see reference to function template instantiation '_Ret std::_Callable_obj<std::_Bind<false,_Ret,_Ty,std::future<std::_Result_of<_Fty,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>,false>::_ApplyX<_Rx,>(void)' being compiled
1> with
1> [
1> _Ret=void
1> , _Ty=then::<lambda_fce0ad2065944df99f1af1e657f83fe2>
1> , _Fty=main::<lambda_671f15aca9913797c9b157283a9bc860>
1> , _Rx=void
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludefunctional(226) : while compiling class template member function 'void std::_Func_impl<_MyWrapper,_Alloc,_Ret,>::_Do_call(void)'
1> with
1> [
1> _Alloc=std::allocator<std::_Func_class<void,>>
1> , _Ret=void
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludefunctional(495) : see reference to class template instantiation 'std::_Func_impl<_MyWrapper,_Alloc,_Ret,>' being compiled
1> with
1> [
1> _Alloc=std::allocator<std::_Func_class<void,>>
1> , _Ret=void
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludefunctional(396) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Do_alloc<_Myimpl,_Ty,_Alloc>(_Fty &&,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Ty=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> , _Alloc=std::allocator<std::_Func_class<void,>>
1> , _Fty=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludefunctional(396) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Do_alloc<_Myimpl,_Ty,_Alloc>(_Fty &&,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Ty=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> , _Alloc=std::allocator<std::_Func_class<void,>>
1> , _Fty=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludefunctional(385) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Reset_alloc<_Ty,std::allocator<std::_Func_class<_Ret,>>>(_Fty &&,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Ty=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> , _Fty=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> , _Alloc=std::allocator<std::_Func_class<void,>>
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludefunctional(385) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Reset_alloc<_Ty,std::allocator<std::_Func_class<_Ret,>>>(_Fty &&,_Alloc)' being compiled
1> with
1> [
1> _Ret=void
1> , _Ty=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> , _Fty=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> , _Alloc=std::allocator<std::_Func_class<void,>>
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludefunctional(671) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Reset<_Ty>(_Fty &&)' being compiled
1> with
1> [
1> _Ret=void
1> , _Ty=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> , _Fty=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludefunctional(671) : see reference to function template instantiation 'void std::_Func_class<_Ret,>::_Reset<_Ty>(_Fty &&)' being compiled
1> with
1> [
1> _Ret=void
1> , _Ty=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> , _Fty=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludefuture(735) : see reference to function template instantiation 'std::function<void (void)>::function<_Ty>(_Fx &&)' being compiled
1> with
1> [
1> _Ty=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> , _Fx=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludefuture(735) : see reference to function template instantiation 'std::function<void (void)>::function<_Ty>(_Fx &&)' being compiled
1> with
1> [
1> _Ty=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> , _Fx=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludefuture(893) : see reference to function template instantiation 'std::_Packaged_state<_Rx (void)>::_Packaged_state<_Ty>(_Fty2 &&)' being compiled
1> with
1> [
1> _Rx=_Ret
1> , _Ty=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> , _Fty2=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludefuture(893) : see reference to function template instantiation 'std::_Packaged_state<_Rx (void)>::_Packaged_state<_Ty>(_Fty2 &&)' being compiled
1> with
1> [
1> _Rx=_Ret
1> , _Ty=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> , _Fty2=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludefuture(1859) : see reference to function template instantiation 'std::_Task_async_state<_Ret,false>::_Task_async_state<_Ty>(_Fty2 &&)' being compiled
1> with
1> [
1> _Ret=_Ret
1> , _Ty=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> , _Fty2=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludefuture(1859) : see reference to function template instantiation 'std::_Task_async_state<_Ret,false>::_Task_async_state<_Ty>(_Fty2 &&)' being compiled
1> with
1> [
1> _Ret=_Ret
1> , _Ty=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> , _Fty2=std::_Bind<false,void,then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<main::<lambda_671f15aca9913797c9b157283a9bc860>,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludefuture(1880) : see reference to function template instantiation 'std::_Associated_state<int> *std::_Get_associated_state<_Ret,std::_Bind<false,void,_Ty,std::future<std::_Result_of<_Fty,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>>(std::_Launch_type,std::_Bind<false,void,_Ty,std::future<std::_Result_of<_Fty,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>> &&)' being compiled
1> with
1> [
1> _Ty=then::<lambda_fce0ad2065944df99f1af1e657f83fe2>
1> , _Fty=main::<lambda_671f15aca9913797c9b157283a9bc860>
1> ]
1> c:program files (x86)microsoft visual studio 12.0vcincludefuture(1897) : see reference to function template instantiation 'std::future<void> std::_Async<then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<_Fty,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>(std::_Launch_type,then::<lambda_fce0ad2065944df99f1af1e657f83fe2> &&,std::future<std::_Result_of<_Fty,>::type> &&,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f> &&)' being compiled
1> with
1> [
1> _Fty=main::<lambda_671f15aca9913797c9b157283a9bc860>
1> ]
1> d:...temp.cpp(12) : see reference to function template instantiation 'std::future<void> std::async<then::<lambda_fce0ad2065944df99f1af1e657f83fe2>,std::future<std::_Result_of<_Fty,>::type>,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>(then::<lambda_fce0ad2065944df99f1af1e657f83fe2> &&,std::future<std::_Result_of<_Fty,>::type> &&,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f> &&)' being compiled
1> with
1> [
1> _Fty=main::<lambda_671f15aca9913797c9b157283a9bc860>
1> ]
1> d:...temp.cpp(26) : see reference to function template instantiation 'std::future<void> then<std::_Result_of<_Fty,>::type,main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>>(std::future<std::_Result_of<_Fty,>::type>,Func)' being compiled
1> with
1> [
1> _Fty=main::<lambda_671f15aca9913797c9b157283a9bc860>
1> , Func=main::<lambda_94e6e075e2832fb9a811ae5846fdb72f>
1> ]
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========
您依赖于在此处复制future<T>
:
template <typename T, typename Func>
auto then(std::future<T> future, Func func)
-> std::future<decltype(func(future))>
你想做的事:
template <typename T, typename Func>
auto then(std::future<T> future, Func func)
-> std::future<decltype(func(std::move(future)))>
相关文章:
- std::is_base_of表示ctor编译错误
- Qt5:使用QCommandLineParser类时出现奇怪的编译错误
- 如何修复sfml c++代码编译错误
- 使用 MATLAB 编码器生成C++代码:编译错误"undefined reference to `rgb2gray_tbb_real64'"
- 使用外部SDK工具链文件在VisualStudio上生成项目编译错误
- vscode下的Arduino代码出现意外编译错误
- 第三方 API 中的编译错误 - Visual Studio
- std::cout输出int时出现编译错误
- 奇怪的代码抛出编译错误模板< J,int aSize=10> C2143:语法错误:在"<"之前缺少";"
- 提升图广度优先搜索前置编译错误
- C++ 中的编译错误:未定义对"主"的引用 collect2:错误:ld 返回 1 个退出状态
- 使用带有 ref 参数的成员函数创建线程时出现编译错误
- 我在C++中遇到了这个奇怪的编译错误
- 在C++中使用 Catch 测试框架编译错误"error: expected ';' at end of declaration list"
- 使用 std::enable_if 限制派生类的模板参数时出现编译错误
- 现代OpenGL和GLEW Libraray的编译错误
- C++ 编译错误:意外的类型名称"字符串":预期的表达式
- C ++程序编译错误,找不到/访问文件
- 使用直接大括号初始化时,C++ 编译错误"声明末尾的预期";"
- 为什么传递非静态成员函数会导致编译错误?