Ghita
Ghita

Reputation: 4505

Why require copy constructor for packaged_task in VS

class MoveOnlyOperation
{
public:
    MoveOnlyOperation()                         = default;
    MoveOnlyOperation(const MoveOnlyOperation&) = delete;
    MoveOnlyOperation(MoveOnlyOperation&&)      = default;

    int operator()()
    {
        return 0;
    }
};

I want to wrap an object instance inside a packaged_task like this:

std::packaged_task<void()> task(MoveOnlyOperation{}); 

I get "error C2280: 'MoveOnlyOperation::MoveOnlyOperation(const MoveOnlyOperation &)': attempting to reference a deleted function"

The documentation for C++ 11 says one can perfect forward the instance inside the packaged_task though. I also don't have issues with clang.

It there something implementation defined about how packaged_task should be implemented or a bug in VS 2015 (and possibly later because I get same issue with http://rextester.com/WBEH22233)

Upvotes: 5

Views: 429

Answers (2)

Val
Val

Reputation: 22797

As @Praetorian said, it's MSVC's known bug.

It caused trouble to me, to put lambda function with moving captured std::unique_ptr, into std::packaged_func. And I'm doing Linux / Windows cross platform, note it works perfectly well in Linux system.

Use its original source: boost::fibers::packaged_task fixed the problem for me.

Upvotes: 0

Praetorian
Praetorian

Reputation: 109159

This is a known bug in MSVC's packaged_task implementation. They're storing the callable within std::function, which requires that the argument be copy-constructible.

Upvotes: 6

Related Questions