4

我有这个最小的不工作的代码示例

#include <future>

int main()
{
    auto intTask = std::packaged_task<int()>( []()->int{ return 5; } );
    std::packaged_task<void()> voidTask{ std::move(intTask) };
}

为什么不编译(在 gcc 4.8.1 上)?我怀疑,原因是,将 lambda 存储在需要参数std::packaged_task的内部。但是,只能移动。这是一个错误吗?标准对此有何评论?在我看来不应该需要争论,但争论应该就足够了。std::functionCopyConstructiblestd::packaged_taskstd::packaged_taskCopyConstructibleMoveConstructible

顺便说一句,当我替换std::packaged_task<int()>一切std::packaged_task<void()>编译正常。

GCC 4.8.1 给了我这个错误信息:

In file included from /usr/include/c++/4.6/future:38:0,
                 from ../cpp11test/main.cpp:160:
/usr/include/c++/4.6/functional: In static member function 'static void        std::_Function_base::_Base_manager<_Functor>::_M_clone(std::_Any_data&, const std::_Any_data&, std::false_type) [with _Functor = std::packaged_task<int()>, std::false_type = std::integral_constant<bool, false>]':
/usr/include/c++/4.6/functional:1652:8:   instantiated from 'static bool std::_Function_base::_Base_manager<_Functor>::_M_manager(std::_Any_data&, const std::_Any_data&, std::_Manager_operation) [with _Functor = std::packaged_task<int()>]'
/usr/include/c++/4.6/functional:2149:6:   instantiated from 'std::function<_Res(_ArgTypes ...)>::function(_Functor, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type) [with _Functor = std::packaged_task<int()>, _Res = void, _ArgTypes = {}, typename std::enable_if<(! std::is_integral<_Functor>::value), std::function<_Res(_ArgTypes ...)>::_Useless>::type = std::function<void()>::_Useless]'
/usr/include/c++/4.6/bits/shared_ptr_base.h:410:4:   instantiated from 'std::_Sp_counted_ptr_inplace<_Tp, _Alloc, _Lp>::_Sp_counted_ptr_inplace(_Alloc, _Args&& ...) [with _Args = {std::packaged_task<int()>}, _Tp = std::__future_base::_Task_state<void()>, _Alloc = std::allocator<std::__future_base::_Task_state<void()> >, __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]'
/usr/include/c++/4.6/bits/shared_ptr_base.h:518:8:   instantiated from 'std::__shared_count<_Lp>::__shared_count(std::_Sp_make_shared_tag, _Tp*, const _Alloc&, _Args&& ...) [with _Tp = std::__future_base::_Task_state<void()>, _Alloc = std::allocator<std::__future_base::_Task_state<void()> >, _Args = {std::packaged_task<int()>}, __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]'
/usr/include/c++/4.6/bits/shared_ptr_base.h:987:35:   instantiated from 'std::__shared_ptr<_Tp, _Lp>::__shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<std::__future_base::_Task_state<void()> >, _Args = {std::packaged_task<int()>}, _Tp = std::__future_base::_Task_state<void()>, __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]'
/usr/include/c++/4.6/bits/shared_ptr.h:317:64:   instantiated from 'std::shared_ptr<_Tp>::shared_ptr(std::_Sp_make_shared_tag, const _Alloc&, _Args&& ...) [with _Alloc = std::allocator<std::__future_base::_Task_state<void()> >, _Args = {std::packaged_task<int()>}, _Tp = std::__future_base::_Task_state<void()>]'
/usr/include/c++/4.6/bits/shared_ptr.h:535:39:   instantiated from 'std::shared_ptr<_Tp> std::allocate_shared(const _Alloc&, _Args&& ...) [with _Tp = std::__future_base::_Task_state<void()>, _Alloc = std::allocator<std::__future_base::_Task_state<void()> >, _Args = {std::packaged_task<int()>}]'
/usr/include/c++/4.6/bits/shared_ptr.h:551:42:   instantiated from 'std::shared_ptr<_Tp1> std::make_shared(_Args&& ...) [with _Tp = std::__future_base::_Task_state<void()>, _Args = {std::packaged_task<int()>}]'
/usr/include/c++/4.6/future:1223:66:   instantiated from 'std::packaged_task<_Res(_ArgTypes ...)>::packaged_task(_Fn&&) [with _Fn = std::packaged_task<int()>, _Res = void, _ArgTypes = {}]'
../cpp11test/main.cpp:165:61:   instantiated from here
/usr/include/c++/4.6/functional:1616:4: error: use of deleted function 'std::packaged_task<_Res(_ArgTypes ...)>::packaged_task(std::packaged_task<_Res(_ArgTypes ...)>&) [with _Res = int, _ArgTypes = {}, std::packaged_task<_Res(_ArgTypes ...)> = std::packaged_task<int()>]'
/usr/include/c++/4.6/future:1244:7: error: declared here

更新:我编写了以下测试程序。这似乎支持了原因缺失的假设CopyConstructabilitystd::packaged_task同样,对可以构造an 的对象的类型有什么要求?

#include <future>

struct Functor {
    Functor() {}
    Functor( const Functor & ) {} // without this line it doesn't compile
    Functor( Functor && ) {}
    int operator()(){ return 5; }
};

int main() {
    auto intTask = std::packaged_task<int()>( Functor{} );
}
4

3 回答 3

2

确实,packaged_task只有一个移动构造函数(30.6.9/2):

template <class F> explicit packaged_task(F&& f);

但是,您的问题是explicit构造函数。所以这样写:

std::packaged_task<int()> pt([]() -> int { return 1; });

完整示例:

#include <future>
#include <thread>

int main()
{
    std::packaged_task<int()> intTask([]() -> int { return 5; } );
    auto f = intTask.get_future();
    std::thread(std::move(intTask)).detach();
    return f.get();
}
于 2013-07-05T17:24:48.687 回答
0

不,您只是不能将 a 移动packaged_task<int ()>到 apackaged_task<void ()>中。这些类型不相关,不能相互移动分配或移动构造。如果你出于某种原因真的想这样做,你可以“吞下”这样int ()结果

于 2013-07-05T17:31:38.873 回答
0

该标准(从 N3690 开始)没有明确说明有关类型要求的任何F内容

template <class R, class... ArgTypes>
template <class F>
packaged_task<R(ArgTypes...)>::packaged_task(F&& f);

(见 30.6.9.1)但是,它指出

调用 的副本的f行为应与调用f.

并且这个电话可以抛出

的复制或移动构造函数引发的任何异常f,或者std::bad_alloc如果无法分配内部数据结构的内存。

这隐含地暗示类型F必须至少为MoveConstructible,或者CopyConstructible,如果将左值引用传递给函数。

因此,这不是一个错误,只是没有精确地指定。要解决将 astd::packaged_task<int()>放入 a的问题,std::packaged_task<void()>只需将第一个放入 a 中,shared_ptr如下所示:

#include <future>
#include <memory>

int main()
{
    auto intTask = std::make_shared<std::packaged_task<int()>>( 
        []()->int{ return 5; } );
    std::packaged_task<void()> voidTask{ [=]{ (*intTask)(); } };
}
于 2013-07-05T18:19:01.543 回答