How does find_type
know where the function typemap
is?
The argument it receives is not from that namespace, it's from the std
namespace!
#include <type_traits>
#include <memory>
namespace lib {
template<typename T>
struct find_type {
using type = decltype(typemap(std::declval<T>()));
};
}
namespace test {
struct Test {};
auto typemap(std::unique_ptr<Test>) -> int;
}
static_assert(std::is_same<int, lib::find_type<std::unique_ptr<test::Test>>::type>::value, "");
How can this code work? What is the rule allowing this?
I tested it with GCC 6.3 and clang 3.9.1.
T
from the structfind_type
isstd::unique_ptr<test::Test>
.std::unique_ptr
considers thetest::Test
template parameter for ADL, and thus, finds thetypemap
function in thetest
NS – Packthread