简体   繁体   English

为什么 `static_pointer_cast` 不能与 ADL 一起使用,但需要显式的 `std::`?

[英]Why doesn't `static_pointer_cast` work with ADL, but requires explicit `std::`?

Consider考虑

// 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);
}

I'd've expected the unqualified call to static_pointer_cast to resolve to std::static_pointer_cast , because b , being a std::shared_ptr , should bring namespace std in using ADL.我希望对static_pointer_cast的不合格调用解析为std::static_pointer_cast ,因为b作为std::shared_ptr应该使用 ADL 将namespace std带入。

Why doesn't it?为什么不呢? I need to write std::shared_pointer_cast explicitly to make it work.我需要明确编写std::shared_pointer_cast以使其工作。

https://en.cppreference.com/w/cpp/language/adl https://en.cppreference.com/w/cpp/language/adl

Although a function call can be resolved through ADL even if ordinary lookup finds nothing, a function call to a function template with explicitly-specified template arguments requires that there is a declaration of the template found by ordinary lookup (otherwise, it is a syntax error to encounter an unknown name followed by a less-than character) (until C++20) Although a function call can be resolved through ADL even if ordinary lookup finds nothing, a function call to a function template with explicitly-specified template arguments requires that there is a declaration of the template found by ordinary lookup (otherwise, it is a syntax error遇到未知名称后跟小于字符)(C++20 前)

In C++20 mode your code compiles fine, demo: https://gcc.godbolt.org/z/b13q4hs68在 C++20 模式下,您的代码编译良好,演示: https://gcc.godbolt.org/z/b13q4hs68

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 std :: move into static_pointer_cast:为什么static_pointer_cast没有rvalue引用重载? - std::move into static_pointer_cast: Why doesn't static_pointer_cast have an rvalue reference overload? static_pointer_cast 调用应该是 std:: 限定的,还是依赖于 ADL? - Should static_pointer_cast calls be std:: qualified, or relied upon ADL? 为什么 std::shared_ptr 在使用 std::static_pointer_cast 时会析构两次? - Why std::shared_ptr destruct twice when used std::static_pointer_cast? std :: static_pointer_cast vs static_cast <std :: shared_ptr <A >> - std::static_pointer_cast vs static_cast<std::shared_ptr<A>> 通过继承和模板进行static_pointer_cast - static_pointer_cast through inheritance and template std :: static_pointer_cast是否有任何额外的运行时开销? - Does std::static_pointer_cast have any additional runtime overhead? 存储 std::weak_ptr<void> 并使用 static_pointer_cast - Storing of std::weak_ptr<void> and using static_pointer_cast 如何在 C++ 中不使用 std::static_pointer_cast 向下转换 shared_ptr? - How to downcast shared_ptr without std::static_pointer_cast in C++? static_cast() 有效但 static_pointer_cast() 无效? - static_cast() works but static_pointer_cast() does not? unique_ptr的static_pointer_cast替代方案 - Alternatives of static_pointer_cast for unique_ptr
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM