ZLMediaKit / ZLToolKit

一个基于C++11的轻量级网络框架,基于线程池技术可以实现大并发网络IO
MIT License
1.94k stars 581 forks source link

The Use of `static_pointer_cast` #195

Closed feixintianxia closed 9 months ago

feixintianxia commented 10 months ago

I would like to ask:

void TcpServer::start_l(uint16_t port, const std::string &host, uint32_t backlog) { //语句A weak_ptr weak_self = std::static_pointer_cast(shared_from_this()); ... //语句B EventPoller::Ptr poller = static_pointer_cast(executor); }

Reference:

https://en.cppreference.com/w/cpp/memory/shared_ptr/pointer_cast

Statement A:

shared_from_this() returns std::shared_ptr<Server> Conversion from base class to derived class should use std::dynamic_pointer_cast to return std::shared_ptr<TcpServer>

Statement B:

Similarly, std::dynamic_pointer_cast needs to be used. I checked the commit history, initially std::dynamic_pointer_cast was used, and later it was changed to static_pointer_cast.

Why?

想咨询一下:

void TcpServer::start_l(uint16_t port, const std::string &host, uint32_t backlog) {
//语句A
weak_ptr<TcpServer> weak_self = std::static_pointer_cast<TcpServer>(shared_from_this());
...
//语句B
EventPoller::Ptr poller = static_pointer_cast<EventPoller>(executor);
}

参考: https://en.cppreference.com/w/cpp/memory/shared_ptr/pointer_cast 语句A: shared_from_this() 返回的是 std::shared_ptr
由基类向派生类转换应该使用std::dynamic_pointer_cast 返回std::shared_ptr

语句B: 同样需要使用std::dynamic_pointer_cast 。 我看了提交记录,最初使用td::dynamic_pointer_cast, 后来才修改为 static_pointer_cast。

为什么呢?

TRANS_BY_GITHUB_AI_ASSISTANT

alexliyu7352 commented 10 months ago

static_pointer_cast does not perform type checking at runtime. Here, the conversion type can be determined, so using static_pointer_cast is more performant.

Mr. Li @.***> wrote on Tuesday, November 28, 2023 at 00:30:

想咨询一下:

void TcpServer::start_l(uint16_t port, const std::string &host, uint32_t backlog) { //语句A weak_ptr weak_self = std::static_pointer_cast(shared_from_this()); ... //语句B EventPoller::Ptr poller = static_pointer_cast(executor); }

参考: https://en.cppreference.com/w/cpp/memory/shared_ptr/pointer_cast 语句A: shared_from_this() 返回的是 std::shared_ptr 由基类向派生类转换应该使用std::dynamic_pointer_cast 返回std::shared_ptr

语句B: 同样需要使用std::dynamic_pointer_cast 。 我看了提交记录,最初使用td::dynamic_pointer_cast, 后来才修改为 static_pointer_cast。

为什么呢?

— Reply to this email directly, view it on GitHub https://github.com/ZLMediaKit/ZLToolKit/issues/195, or unsubscribe https://github.com/notifications/unsubscribe-auth/AF3RPRL7SPR2HLOOTQOWBWTYGS5Z3AVCNFSM6AAAAAA74J7BDCVHI2DSMVQWIX3LMV43ASLTON2WKOZSGAYTENRVGYZTENY . You are receiving this because you are subscribed to this thread.Message ID: @.***>

static_pointer_cast不在运行时做类型检查, 这里可以确定转换类型, 所以使用static_pointer_cast性能更好.

Mr. Li @.***> 于2023年11月28日周二 00:30写道:

想咨询一下:

void TcpServer::start_l(uint16_t port, const std::string &host, uint32_t backlog) { //语句A weak_ptr weak_self = std::static_pointer_cast(shared_from_this()); ... //语句B EventPoller::Ptr poller = static_pointer_cast(executor); }

参考: https://en.cppreference.com/w/cpp/memory/shared_ptr/pointer_cast 语句A: shared_from_this() 返回的是 std::shared_ptr 由基类向派生类转换应该使用std::dynamic_pointer_cast 返回std::shared_ptr

语句B: 同样需要使用std::dynamic_pointer_cast 。 我看了提交记录,最初使用td::dynamic_pointer_cast, 后来才修改为 static_pointer_cast。

为什么呢?

— Reply to this email directly, view it on GitHub https://github.com/ZLMediaKit/ZLToolKit/issues/195, or unsubscribe https://github.com/notifications/unsubscribe-auth/AF3RPRL7SPR2HLOOTQOWBWTYGS5Z3AVCNFSM6AAAAAA74J7BDCVHI2DSMVQWIX3LMV43ASLTON2WKOZSGAYTENRVGYZTENY . You are receiving this because you are subscribed to this thread.Message ID: @.***>

TRANS_BY_GITHUB_AI_ASSISTANT