微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

为什么 `static_pointer_cast` 不适用于 ADL,而是需要显式的 `std::`?

如何解决为什么 `static_pointer_cast` 不适用于 ADL,而是需要显式的 `std::`?

考虑

// https://godbolt.org/z/z5M9b9jzx
#include <memory>
#include <cassert>

struct B {};
struct D : B {};

int main() {
    std::shared_ptr<B> b = std::make_shared<D>();
    auto d = static_pointer_cast<D>(b);
    assert(d);
}

我原以为对 static_pointer_cast 的不合格调用会解析为 std::static_pointer_cast,因为 b 作为 std::shared_ptr,应该将 namespace std 引入使用 ADL。

为什么不呢?我需要明确地编写 std::shared_pointer_cast 才能使其工作。

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。