Search code examples
c++c++17perfect-forwarding

error perfect forwarding a lamda - discards qualifiers


Given this code: link

I am passing a lambda and a parameter pack and forwarding it to std::thread's constructor.

Here is a code snippet:

template<typename Functor, class... Args>
std::thread create_fifo_thread(int sch_priority, const Functor&& func, Args&&... args)
{
    std::thread th(std::forward<Functor>(func), std::forward<Args>(args)...)
//    std::thread th(func, std::forward<Args>(args)...);
    set_fifo_pri(th, sch_priority);
    return th;
}

You can see when I do it like this I get the error, but when I switch the commented line in it works fine. Please follow the link to see the error its quite long! - but I can print here if needd.

Here is how I call it:

auto fth = create_fifo_thread(10, [](int count){ return test(count); }, 10);

Note that test() is void test(int count) - very trivial function.

My questions are/is: do I need to try to perfect forward here? - and what qualifiers am I discarding? - what is the best way to do this?


Solution

  • To perfect forwarding use Functor&& func - not const Functor&& func