1

As I want to give my async, reactor style pattern, callbacks a result in form of a future from boost::asio, I came across a compile error, i.e. constness clash I don't understand:

When I want to call a packed task in intention to fulfill the underlying promise i.e. make the attached future ready, via a lambda I got constness this compiler errors.

#include <future>
#include <utility>

int main() {
    std::packaged_task<int()> task{
        [] () {
            return 5;
        }
    };
    auto f = [task2 = std::move(task)] () {
        task2();
    };
    f();
}

The call of task2 operator() fails on my gcc compiler with:

/home/markus/CLionProjects/untitled5/main.cpp:12:15: error: no match for call to ‘(const std::packaged_task) ()’ 12 |
task2(); | ^ In file included from /home/markus/CLionProjects/untitled5/main.cpp:1: /usr/include/c++/9.3.0/future:1548:7: note: candidate: ‘void std::packaged_task<_Res(_ArgTypes ...)>::operator()(_ArgTypes ...) [with _Res = int; _ArgTypes = {}]’ 1548 |
operator()(_ArgTypes... __args) | ^~~~~~~~ /usr/include/c++/9.3.0/future:1548:7: note: passing ‘const std::packaged_task*’ as ‘this’ argument discards qualifiers

If I read this correctly the packaged_task has suddenly become a const one, so the compiler errors that the packed_task::operator() method is not marked as const. The only thing I did between task and task2 is calling the move ctor packaged_task( packaged_task&& rhs ) noexcept;, which should AFAIK not change the constness. Obviously I am missing something essential.

Superlokkus
  • 4,731
  • 1
  • 25
  • 57

0 Answers0