Std :: packaged_task compilation error w / gcc 4.6

I am trying to run a function in a stream using std::packaged_task

Query query; /* protobuf object */        

/* fill Query object */

std::packaged_task<SearchResults(Query&)> task([](Query& q) ->SearchResults {
    index::core::Merger merger;
    return merger.search(q);
    });

std::future<SearchResults> ftr = task.get_future();
std::thread(std::move(task),query).detach();

Edit2: updated the code again to fix the errors and included the full error message.

g ++ - 4.6 (according to Ubuntu 10.04) cannot compile code:

In file included from /usr/include/c++/4.6/memory:80:0,
              from ../src/net/QueryConnection.cpp:8:
/usr/include/c++/4.6/functional: In instantiation of ‘std::_Bind_result<void, 
std::packaged_task<SearchResults(Query&)>(Query)>’:
/usr/include/c++/4.6/thread:135:9:   instantiated from ‘std::thread::thread(_Callable&&, 
_Args&& ...) [with _Callable = std::packaged_task<SearchResults(Query&)>, _Args = 
{Query&}]’
../src/net/QueryConnection.cpp:77:36:   instantiated from here
/usr/include/c++/4.6/functional:1365:7: error: ‘std::_Bind_result<_Result, 
_Functor(_Bound_args ...)>::_Bind_result(const std::_Bind_result<_Result, 
_Functor(_Bound_args ...)>&) [with _Result = void, _Functor =   
std::packaged_task<SearchResults(Query&)>, _Bound_args = {Query}, 
std::_Bind_result<_Result, _Functor(_Bound_args ...)> = std::_Bind_result<void, 
std::packaged_task<SearchResults(Query&)>(Query)>]’ declared to take const reference, 
but implicit declaration would take non-const
Build error occurred, build is stopped

I read that this is possible due to an error: gcc-mailinglist

I'm new to C ++ / C ++ 11 - What would be a good working alternative? I just need to start the thread that gives me the future — the get()method is called later in the async loop boost::asio.

+5
source share
3 answers

This is a bug in GCC 4.6 (actually a defect in the C ++ 11 standard), which I have already fixed in 4.7.

As a workaround you can use std::async

Query query;
std::future<SearchResults> ftr = std::async([](Query& q) ->SearchResults {
      index::core::Merger merger;
      return merger.search(q);
    }, query);

GCC 4.6, packaged_task .

+3

, , GCC , .

[=](Query& q){
        index::core::Merger merger;
        return merger.search(q);
}

return , void. , SearchResults. Query&, SearchResults(Query&).

[=](Query& q) -> SearchResults {
        index::core::Merger merger;
        return merger.search(q);
}
+1

:

1) . , ( , ):

[](Query& q) -> SearchResults {
        index::core::Merger merger;
        return merger.search(q);
}

2) , Query&, std::thread, ,

, , , std::thread, std::ref(q), .

, ,

/usr/include/++/4.6/future: 1272: 7: note: 1 , 0

0

All Articles