argument dependent lookup not considered


This question already has an answer here:


In order to even understand that you have a function call with a template, and not a bunch of < and > operators, the compiler must know you have a function template; and in order to understand that, it must know what namespace to look it up in. And in order to know that, it must understand the namespaces of the function arguments. And in order to understand that it must know there are function arguments around. Which, as we have seen, depends on knowing that there's a function call to begin with. Which the compiler doesn't know until it finds a template declaration. See the problem?

Because of that, ADL is only ever considered if the postfix-expression in the function call is an unqualified-id. Which dynamicCast<Test::Derived> is not <edit> only if dynamicCast is known to be a template-name, which is determined during normal unqualified lookup, which doesn't look into the namespace where the template is declared.

As @T.C. observed, one can declare an unrelated function template named dynamicCast in the global namespace in order to make ADL work.


In a better world we would have an option to write template foo<whatever> in any context and disambiguate the angle brackets. Maybe in C++20.

Template functions with explicit template arguments are not found via ADL. Maybe a parsing problem, do not know why.

Another example is std::get. You cannot get<3>(some_tuple) without a using statement.

You can work around this by passing the arguments you would pass as template args as tag types. You can also do a one-two step external function plus internal ADL tag dispatched lookup (so the public function has to be qualified, but customization points of the internal tag-based ADL can be done).

// tag utilities:
template<class T>struct tag_t{using type=T;constexpr tag_t(){};};
template<class T>constexpr tag_t<T> tag={};

namespace some{
  template<class T, class U>
  T* dynamic(tag_t<T>, U* u){
    return dynamic_cast<T>(u);
  struct bob{};

Now dynamic(tag<int>,new some::bob{}) will find dynamic via ADL.

I do not have chapter and verse from the standard for you.

Need Your Help

How can I determine whether my installer is running on the Education edition of Windows 10?

c# c++ windows-10

When I run winver on Windows 10 Education the popup says it is the Education edition, so there must be some way to determine this programatically. I've searched on the internet and cannot find any

Python SSL requests and Let's Encrypt certs

python ssl openssl python-requests lets-encrypt

I'm struggling at the moment to get the requests library to perform a simple GET request to a site of mine with a Let's Encrypt certificate. All's well with the site and I can access it from Chrome...