[英]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.