vit-vit / CTPL

Modern and efficient C++ Thread Pool Library
Apache License 2.0
1.81k stars 335 forks source link

Problem in passing my function to "push" method #28

Closed atari83 closed 1 year ago

atari83 commented 4 years ago

Hello there,

So, i have a function with this definition: static void Invoke( int id, std::unique_ptr<BaseService> svc );

And tried to pass it to ctpl "push" method to be queued in thread-pool: pThreadPool->push( std::ref(App::Invoke), std::move( svc ) );

But I get 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.
smithnote commented 4 years ago

you could change function's definition(pass by reference):

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

and try

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