Fed*_*dor 7 c++ argument-dependent-lookup c++20
下面的程序在 C++20 中编译得很好:
#include <memory>
struct A{ virtual ~A() = default; };
struct B: A {};
int main()
{
std::shared_ptr<A> p = std::make_shared<B>();
auto x = dynamic_pointer_cast<A>(p);
}
Run Code Online (Sandbox Code Playgroud)
但是在 C++17 中它会产生一个错误:
<source>: In function 'int main()':
<source>:9:14: error: 'dynamic_pointer_cast' was not declared in this scope; did you mean 'std::dynamic_pointer_cast'?
9 | auto x = dynamic_pointer_cast<A>(p);
| ^~~~~~~~~~~~~~~~~~~~
| std::dynamic_pointer_cast
In file included from /opt/compiler-explorer/gcc-11.1.0/include/c++/11.1.0/memory:77,
from <source>:1:
Run Code Online (Sandbox Code Playgroud)
你能告诉我在 C++20 中有什么改变使它工作吗?
https://en.cppreference.com/w/cpp/language/adl
虽然通过ADL可以解决函数调用即使普通查找什么也没找到,但是对带有显式指定模板参数的函数模板的函数调用需要有普通查找找到的模板的声明(否则就是语法错误遇到未知名称后跟小于字符)(直到 C++20)