v2ex帖子
利用adl改造
namespace NS
{
struct Test{};
void f(int){}
void f(Test){}
}
int main()
{
f(1);//error
f(NS::Test());//works,because of ADL
}
template<unsigned P> struct priority : priority<P-1> {};
template<> struct priority<0> {};
template<class T>
auto checkNamespaceFuncWithADL(T i,priority<1>) -> decltype(f(i)) { f(i); }
template<class T>
void checkNamespaceFuncWithADL(T i,priority<0>) { static_assert(sizeof(T)==0,"couldn't find a function f in T namespace"); }
template<class T>
void checkNamespaceFuncWithADL(T i){checkNamespaceFuncWithADL(i,priority<1>{});}
namespace NS
{
struct Test{};
}
void f(NS::Test){std::cout<<"HAHA";}
int main()
{
checkNamespaceFuncWithADL(NS::Test());//error
}
template<unsigned P> struct priority : priority<P-1> {};
template<> struct priority<0> {};
template<class T>
auto checkNamespaceFuncWithADL(T i,priority<1>) -> decltype(f(i)) { f(i); }
template<class T>
void checkNamespaceFuncWithADL(T i,priority<0>) { static_assert(sizeof(T)==0,"couldn't find a function f in T namespace"); }
template<class T>
void checkNamespaceFuncWithADL(T i){checkNamespaceFuncWithADL(i,priority<1>{});}
namespace NS
{
struct Test{};
void f(Test){std::cout<<"HAHA";}
}
int main()
{
checkNamespaceFuncWithADL(NS::Test());//works!
}