我正在使用 Boost 庫在 C 中創建一個 web-socket 服務器。我的出發點是來自這個站點的 Boost 示例。我對方法中的這部分代碼有疑問on_run:
ws_.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res)
{
res.set(http::field::server,
std::string(BOOST_BEAST_VERSION_STRING)
" websocket-server-async");
}));
可以看出,ws_.set_option將 awebsocket::stream_base::decorator作為引數,該引數由 lambda 運算式創建,該運算式Decorator&&根據此站點將型別變數作為引數。
Decorator&&當 lambda 運算式中沒有 trailing-return-type 或 return 陳述句時,lambda 運算式如何知道它應該回傳 a ?最后但并非最不重要的一點是,websocket::response_type &res從哪里來?在 lambda 主體之后沒有() 包含此引數。
uj5u.com熱心網友回復:
websocket::stream_base::decorator 是具有一個模板型別引數的模板函式。
template<class Decorator>
decorator( Decorator&& f );
在這次通話中
ws_.set_option(websocket::stream_base::decorator(
[](websocket::response_type& res)
{
res.set(http::field::server,
std::string(BOOST_BEAST_VERSION_STRING)
" websocket-server-async");
}));
該函式被稱為接受 lambda 運算式
[](websocket::response_type& res)
{
res.set(http::field::server,
std::string(BOOST_BEAST_VERSION_STRING)
" websocket-server-async");
}
作為它的模板引數。lambda 運算式默認具有回傳型別void,因為 lambda 運算式中沒有 return 陳述句。
lambda運算式怎么知道,它應該回傳一個Decorator&&
它是 lambda 運算式本身用作宣告的函式模板引數的引數 Decorator&&。
uj5u.com熱心網友回復:
websocket::stream_base::decorator(...呼叫decorator. 此建構式顯然將可呼叫物件作為引數。通過的 lamdba 沒有回報。它的回傳型別是void.
見這里(一些程式員帥哥評論的鏈接):https ://www.boost.org/doc/libs/1_78_0/libs/beast/doc/html/beast/ref/boost__beast__websocket__stream_base__decorator/decorator/overload2.html 。
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/415745.html
標籤:
