简体   繁体   中英

“call to implicitly-deleted copy constructor of” error when tried to pass argument to a method

I'm, trying to use CPTL thread-pool for my application.

So, i have a function with this definition:

static void Invoke( int id, std::unique_ptr<BaseService> svc );

And tried to pass it to CPTL "push" method to be queued in thread-pool:

pThreadPool->push( std::ref(App::Invoke), std::move( svc ) );

But I received this error:

/home/hadi/CLionProjects/App/App.cpp:211:27: error: no matching member function for call to 'push'
    pThreadPool->push( std::ref(App::Invoke), std::move( svc ) );
    ~~~~~~~~~~~~~^~~~
/home/hadi/CLionProjects/App/include/cptl/ctpl.h:152:14: note: candidate template ignored: substitution failure [with F = std::__1::reference_wrapper<void (int, std::__1::unique_ptr<BaseService, std::__1::default_delete<BaseService> >)>, Rest = <std::__1::unique_ptr<BaseService, std::__1::default_delete<BaseService> >>]: call to implicitly-deleted copy constructor of 'std::__1::unique_ptr<BaseService, std::__1::default_delete<BaseService> >'
        auto push(F && f, Rest&&... rest) ->std::future<decltype(f(0, rest...))> {
             ^                                                        ~~~~
/home/hadi/CLionProjects/App/include/cptl/ctpl.h:171:14: note: candidate function template not viable: requires single argument 'f', but 2 arguments were provided
        auto push(F && f) ->std::future<decltype(f(0))> {
             ^
1 error generated.

Can anybody please tell me how to fix this? Thanks.

It seems CPTL either has a bug at the line 152 or doesn't support move-only arguments (can't find any documentation on it so can't be sure):

    auto push(F && f, Rest&&... rest) ->std::future<decltype(f(0, rest...))> {

Even though push uses std::forward<Rest>(rest)... in the invocation, it uses rest... in the SFINAE, which fails and excludes the overload from the list of viable candidates.

Normally the arguments of packaged tasks are stored in a queue, and thus are required to be copyable. That precludes the use of unique_ptr since it's not copyable (it's unique.).

As a workaround you can use a shared_ptr or, if the lifetime of svc exceeds the lifetime of the thread pool, a raw pointer.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM