dimanche 4 septembre 2016

Using of std vector emplace_back

I have some class-warper for std thread. Here is constructor:

template <typename Function, typename... Args>
InterruptibleThread(Function&& fun, Args&&... args)
{
    _thread = std::thread([](std::atomic_bool * f, Function&& function, Args&&... arguments)
    {
        _flag_ref = f;
        (function)(std::forward<Args>(arguments)...);
    },
        &_flag,
        std::forward<Function>(fun)
        , std::forward<Args>(args)...
        );
}

And then I'm using it so (example):

InterruptibleThread(&SourceImageList::StartFrameProcessingStatic, this, std::ref(it))

The compiler build this code successfuly. But now I would like to make a vector of such objects:

std::vector<InterruptibleThread> grp;

I would like to allocate it in the stack so what I'm doing is:

grp.emplace_back(&SourceImageList::StartFrameProcessingStatic, this, std::ref(it));

And I'm getting this error:

C2064   term does not evaluate to a function taking 0 arguments

Here is options which are validated by compiler:

1) grp.push_back(new InterruptibleThread(&SourceImageList::StartFrameProcessingStatic, this, std::ref(it)));
2) grp.push_back(InterruptibleThread(&SourceImageList::StartFrameProcessingStatic, this, std::ref(it)));

But first one is allocating an object in the heap so I need to release it manually, and second one makes a copy of the object.

Can I use emplace_back here (compiler is MSVC 2015 update 3)?

Aucun commentaire:

Enregistrer un commentaire