如何在模板函数中使用enable_shared_from_this?
How to use enable_shared_from_this in template functions?
我正在尝试在模板函数中使用 std::enabled_shared_from_this 函数,但我在 class 中调用 shared_from_this() 时不断收到消息 bad_weak_ptr 错误。
template<T>
class A: public std::enable_shared_from_this <A<T>>
{
/* some parts in the class */
void Run()
{
resolver_.async_resolve(host_, port_, boost::bind_front_handler(&A:on_resolve, this->shared_from_this());
}
/* ... */
};
我试过 std::enable_shared_from_this>::shared_from_this() 和 this->shared_from_this() 但我没有在 shared_from_this() 中调用函数 class构造函数。如果有任何方法可以解决此错误,请告诉我。
您的查询有误,you're passing too many arguments。相反,传递单个查询参数:
boost::asio::ip::tcp::resolver::query q{host_, port_};
resolver_.async_resolve(q,
接下来,您可以使用您喜欢的任一绑定方法:
boost::bind(&A::on_resolve, this->shared_from_this(),
boost::asio::placeholders::error(),
boost::asio::placeholders::results())
或者根本不使用绑定:
resolver_.async_resolve(q,
[self=this->shared_from_this()](boost::system::error_code ec, boost::asio::ip::tcp::resolver::results_type eps) {
self->on_resolve(ec, eps);
}
);
或者使用 Beast 的便捷助手:
boost::beast::bind_front_handler(&A::on_resolve, this->shared_from_this())
现场演示
#include <memory> // for std::shared_from_this etc
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp> // for boost::bind
#include <boost/beast/core/bind_handler.hpp> // for bind_front_handler
template<typename T>
class A: public std::enable_shared_from_this<A<T>>
{
public:
A(boost::asio::io_context& io, std::string host, std::string port)
: resolver_(io), host_(host), port_(port) {}
void Run()
{
boost::asio::ip::tcp::resolver::query q{host_, port_};
resolver_.async_resolve(q,
boost::beast::bind_front_handler(&A::on_resolve, this->shared_from_this())
);
resolver_.async_resolve(q,
boost::bind(&A::on_resolve, this->shared_from_this(),
boost::asio::placeholders::error(),
boost::asio::placeholders::results())
);
resolver_.async_resolve(q,
[self=this->shared_from_this()](boost::system::error_code ec, boost::asio::ip::tcp::resolver::results_type eps) {
self->on_resolve(ec, eps);
}
);
}
private:
void on_resolve(boost::system::error_code ec, boost::asio::ip::tcp::resolver::results_type eps) {
std::cout << "Resolved: (" << ec.message() << "):";
for (auto& ep : eps) {
std::cout << " " << ep.endpoint();
}
std::cout << "\n";
}
boost::asio::ip::tcp::resolver resolver_;
std::string host_, port_;
};
int main() {
boost::asio::io_context io;
auto instance = std::make_shared<A<int> >(io, "localhost", "1234");
instance->Run();
io.run();
}
打印(由于网络限制,不在 Coliru 上):
Resolved: (Success): 127.0.0.1:1234
Resolved: (Success): 127.0.0.1:1234
Resolved: (Success): 127.0.0.1:1234
我正在尝试在模板函数中使用 std::enabled_shared_from_this 函数,但我在 class 中调用 shared_from_this() 时不断收到消息 bad_weak_ptr 错误。
template<T>
class A: public std::enable_shared_from_this <A<T>>
{
/* some parts in the class */
void Run()
{
resolver_.async_resolve(host_, port_, boost::bind_front_handler(&A:on_resolve, this->shared_from_this());
}
/* ... */
};
我试过 std::enable_shared_from_this>::shared_from_this() 和 this->shared_from_this() 但我没有在 shared_from_this() 中调用函数 class构造函数。如果有任何方法可以解决此错误,请告诉我。
您的查询有误,you're passing too many arguments。相反,传递单个查询参数:
boost::asio::ip::tcp::resolver::query q{host_, port_};
resolver_.async_resolve(q,
接下来,您可以使用您喜欢的任一绑定方法:
boost::bind(&A::on_resolve, this->shared_from_this(),
boost::asio::placeholders::error(),
boost::asio::placeholders::results())
或者根本不使用绑定:
resolver_.async_resolve(q,
[self=this->shared_from_this()](boost::system::error_code ec, boost::asio::ip::tcp::resolver::results_type eps) {
self->on_resolve(ec, eps);
}
);
或者使用 Beast 的便捷助手:
boost::beast::bind_front_handler(&A::on_resolve, this->shared_from_this())
现场演示
#include <memory> // for std::shared_from_this etc
#include <iostream>
#include <boost/asio.hpp>
#include <boost/bind.hpp> // for boost::bind
#include <boost/beast/core/bind_handler.hpp> // for bind_front_handler
template<typename T>
class A: public std::enable_shared_from_this<A<T>>
{
public:
A(boost::asio::io_context& io, std::string host, std::string port)
: resolver_(io), host_(host), port_(port) {}
void Run()
{
boost::asio::ip::tcp::resolver::query q{host_, port_};
resolver_.async_resolve(q,
boost::beast::bind_front_handler(&A::on_resolve, this->shared_from_this())
);
resolver_.async_resolve(q,
boost::bind(&A::on_resolve, this->shared_from_this(),
boost::asio::placeholders::error(),
boost::asio::placeholders::results())
);
resolver_.async_resolve(q,
[self=this->shared_from_this()](boost::system::error_code ec, boost::asio::ip::tcp::resolver::results_type eps) {
self->on_resolve(ec, eps);
}
);
}
private:
void on_resolve(boost::system::error_code ec, boost::asio::ip::tcp::resolver::results_type eps) {
std::cout << "Resolved: (" << ec.message() << "):";
for (auto& ep : eps) {
std::cout << " " << ep.endpoint();
}
std::cout << "\n";
}
boost::asio::ip::tcp::resolver resolver_;
std::string host_, port_;
};
int main() {
boost::asio::io_context io;
auto instance = std::make_shared<A<int> >(io, "localhost", "1234");
instance->Run();
io.run();
}
打印(由于网络限制,不在 Coliru 上):
Resolved: (Success): 127.0.0.1:1234
Resolved: (Success): 127.0.0.1:1234
Resolved: (Success): 127.0.0.1:1234