Skip to content

Instantly share code, notes, and snippets.

@ChristianIvicevic
Created March 2, 2021 13:53
Show Gist options
  • Save ChristianIvicevic/27ace3ab944e6fd7f98838fb4440ffbc to your computer and use it in GitHub Desktop.
Save ChristianIvicevic/27ace3ab944e6fd7f98838fb4440ffbc to your computer and use it in GitHub Desktop.
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