Consider the following C++ program:
#include <memory>
struct A {};
struct B : A {};
int main()
{
auto x = std::make_shared<A>();
if (auto p = dynamic_pointer_cast<B>(x));
}
When compiling with MSVC 2010, I obtain the following error:
error C2065: 'dynamic_pointer_cast' : undeclared identifier
The error persists if auto is replaced by std::shared_ptr<A>. When I fully qualify the call with std::dynamic_pointer_cast, the program successfully compiles.
Also, gcc 4.5.1 doesn’t like it either:
error: 'dynamic_pointer_cast' was not declared in this scope
I thought that std::dynamic_pointer_cast would have been picked by Koenig lookup, since the type of x lives in the std namespace. What am I missing here ?
I think section §14.8.1/6 (C++03, and I think it holds in C++11 also) applies to this case which reads as,
Your case do not trigger ADL because you explicitly pass template argument and there is no template with the same name available at the site where you call
dynamic_pointer_cast.One trick to enable ADL is to add a dummy template with same name to your code, as shown below: