Created
March 2, 2021 13:53
-
-
Save ChristianIvicevic/27ace3ab944e6fd7f98838fb4440ffbc to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class TcpConnection : public std::enable_shared_from_this<TcpConnection> { | |
public: | |
auto start() -> void { | |
// (1) Redudant declaration of paramters and types | |
asio::async_write( | |
Socket, asio::buffer(Response), | |
[This = shared_from_this()](const asio::error_code &ErrorCode, | |
size_t TransferredBytes) -> void { | |
This->handleWrite(ErrorCode, TransferredBytes); | |
}); | |
// (2) Obnoxious auto/decltype magic | |
asio::async_write( | |
Socket, asio::buffer(Response), | |
[This = shared_from_this()](auto &&ErrorCode, | |
auto &&TransferredBytes) -> void { | |
This->handleWrite( | |
std::forward<decltype(ErrorCode)>(ErrorCode), | |
std::forward<decltype(TransferredBytes)>(TransferredBytes)); | |
}); | |
// (3) Auto magic that seems error-prone | |
asio::async_write(Socket, asio::buffer(Response), | |
[This = shared_from_this()]( | |
auto &&ErrorCode, auto &&TransferredBytes) -> void { | |
This->handleWrite(ErrorCode, TransferredBytes); | |
}); | |
// (4) Using std::bind even though clang-tidy suggests using a lambda | |
// instead | |
asio::async_write(Socket, asio::buffer(Response), | |
std::bind(&TcpConnection::handleWrite, shared_from_this(), | |
std::placeholders::_1, std::placeholders::_2)); | |
} | |
private: | |
auto handleWrite(const asio::error_code &ErrorCode, size_t TransferredBytes) | |
-> void{}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment