1
此問題涉及boost::asio
,但純粹是C++ 11
問題。將具有返回值的lambda傳遞迴調回調沒有返回值
我是新來C++ 11
& lambda
技術,而我試圖用boost::asio::async_connect
用於網絡通信。
以下是我嘗試與主機異步連接的功能。
bool MyAsyncConnectFunction() {
//some logic here to check validity of host
if (ip_is_not_resolved)
return false;
the_socket.reset(new tcp::socket(the_io_service));
auto my_connection_handler = [this]
(const boost::system::error_code& errc, const tcp::resolver::iterator& itr)
{
if (errc) {
//Set some variables to false as we are not connected
return false;
}
//Do some stuff as we are successfully connected at this point
return true;
};
//How is async_connect taking a lambda which
boost::asio::async_connect(the_socket, IP_destination, tcp::resolver::iterator(), my_connection_handler);
return true;
}
所有工作正常。沒有絕對的功能問題。但是,我想知道boost::asio::async_connect
在最後一個參數中需要ConnectionHandler without a return type
,但我傳遞了一個lambda,即返回一個值的my_connection_handler
。
我怎樣才能傳遞一個返回值的lambda,而boost::asio::async_connect
的第四個參數是否需要一個沒有返回值的回調?
返回值可以在這個簡單的例子中被丟棄:http://coliru.stacked-crooked.com/a/7d451892e201ba84 在asio中也是這樣做的:https://github.com/boostorg/asio/ blob/42e7869f411a75512fb6994c634eb086fb9eb5cc/include/boost/asio/impl/connect.hpp#L258 –
http://www.boost.org/doc/libs/1_62_0/doc/html/boost_asio/reference/asynchronous_operations.html#boost_asio.reference。 asynchronous_operations.return_type_of_an_initiating_function – Arunmu
@ms返回值被丟棄!這是否意味着我可以將基於返回值的回調綁定到無效的回調函數?或者,這是asio的connect_handler寫入的方式嗎?正如我所說我是新手。對不起,如果它的一個愚蠢的問題 –