8

The program as follows compiles fine in 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);
}

But in C++17 it produces an error:

<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:

Could you please tell me what has changed in C++20 to make it work?

Fedor
  • 17,146
  • 13
  • 40
  • 131
  • 3
    You may want the `language-lawyer` tag on this question. – Eljay Jun 14 '21 at 18:46
  • 4
    This might be the relevant paper: http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2017/p0846r0.html – Fatih BAKIR Jun 14 '21 at 18:48
  • 1
    Clang has a warning for this in C++17 mode, and possibly the explanation: `warning: use of function template name with no prior declaration in function call with explicit template arguments is a C++20 extension [-Wc++20-extensions]` – Joel Filho Jun 14 '21 at 18:48

1 Answers1

11

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)

NathanOliver
  • 171,901
  • 28
  • 288
  • 402
Daniel
  • 30,896
  • 18
  • 85
  • 139
  • 1
    This text is confusing: it means to say "requires that there is a declaration of **a** template". It doesn't have to see the template that it's gonna call. ADL is still done even for template ids. – Johannes Schaub - litb Jun 14 '21 at 18:58