0

I am creating a "job" that store unique pointers to basic "tasks"

    class JobBase
    {
    protected:

        template<typename... BaseOperations>
        JobBase(const std::wstring& operationDescription, BaseOperations&&... args)
            : m_operationDescription(operationDescription)
            , m_BaseOperations{std::move<BaseOperations>(args)...}
        {
        }

    private:

        std::vector<std::unique_ptr<Task>> m_BaseOperations;
}

And create job with few tasks:

std::unique_ptr<Task> pTask = std::make_unique<Task>();
auto asyncCompOp2 = std::make_shared<Job>(L"AsyncOp", std::move(pTask));

But when I build my program I receive error:

Error   C2664   'std::unique_ptr<Task,std::default_delete<_Ty>> &&std::move<std::unique_ptr<_Ty,std::default_delete<_Ty>>>(std::unique_ptr<_Ty,std::default_delete<_Ty>> &&) noexcept': cannot convert argument 1 from '_Ty' to '_Ty &&'

And I don't understant what is wrong. I created classic constructor, that accepts unique_ptr and emplaced passed value to vector and all working good. Task class now doesn't have members, dtor, copy/move operators and ctors. Please, help me undestand what I need to do

Full code sample

#include <memory>
#include <vector>
#include <string>

class Task
{
public:
    void Run() {}
};

class Job
{
public:
    template<typename... BaseOperations>
    Job(const std::wstring& name, BaseOperations&&... args)
        : m_Name(name)
        , m_BaseOperations{ std::forward<BaseOperations>(args)... }
    {
    }
private:
    std::wstring m_Name;
    std::vector<std::unique_ptr<Task>> m_BaseOperations;
};

int main()
{
    std::unique_ptr<Task> pOp3 = std::make_unique<Task>();
    auto asyncCompOp2 = std::make_shared<Job>(L"AsyncOp", std::move(pOp3));
}
drem1lin
  • 331
  • 1
  • 3
  • 14
  • there's no `std::vector` constructor just taking elements right? There is one taking an initializer list of elements though, maybe try calling that one – PeterT May 13 '21 at 15:02
  • @PeterT, yes, thank you, i found this issue. But for forward error - deleted function, for move - cannot convert Ty to Ty&& – drem1lin May 13 '21 at 15:07
  • 1
    Prefer to `std::forward` your received variadic arguments instead of `std::move`; the difference is subtle but important. – AndyG May 13 '21 at 15:08
  • @AndyG Thank you, I tried forward - but "attemting to reference a deleted function" error occurs – drem1lin May 13 '21 at 15:11
  • 1
    @drem1lin that was just generic advice. We really can't help you until you provide a [mcve] – AndyG May 13 '21 at 15:16
  • 2
    `initializer_list` basically doesn't support move semantics, it always copies. So there isn't really a way to list initialize an `std::vector>`. It can be very annoying. – François Andrieux May 13 '21 at 15:24
  • The error is complaining about `std::move`, right? But the argument shown is not of type _Ty but a unique_pointer. Strange. Anyway, try writing it on one line directly without the variable that needs moving from: `std::make_shared(L"AsyncOp", std::make_unique());` – JDługosz May 13 '21 at 15:27

0 Answers0