I have run into a situation that looks like this. I am using g on Windows 10.
#include <stdio.h>
template<typename _t>
struct test_thing {};
template<typename _t> void test_2(_t) { printf("A"); }
template<typename _t>
void test()
{
//test_2(_t{}); // prints: ABC
::test_2(_t{}); // prints: ABA <-- namespace op, searching different?
}
template<> void test_2(double) { printf("B"); }
template<typename _t> void test_2(test_thing<_t>) { printf("C"); }
int main()
{
test<int>();
test<double>();
test<test_thing<int>>();
return 0;
}
My question is why/how is the namespace op changing how the compiler is searching for the function to call. Doesn't the compiler find the most specialized template function that matches the args? If test_2(test_thing<_t>)
is defined above test
it finds it, but only with the ::
, not below.
CodePudding user response:
Doesn't the compiler find the most specialized template function that matches the args?
Function templates can't be partial specialized. The last test_2
is overloaded with the 1st one.
template<typename _t> void test_2(_t) { printf("A"); } // overload #1
template<typename _t>
void test()
{
//test_2(_t{}); // prints: ABC
::test_2(_t{}); // prints: ABA <-- namespace op, searching different?
}
template<> void test_2(double) { printf("B"); } // explicit specialization for overload#1
template<typename _t> void test_2(test_thing<_t>) { printf("C"); } // overload #2
Given test_2(_t{});
, ADL helps and finds the 2nd overloaded test_2
. For ::test_2(_t{});
ADL won't take effect, only the 1st test_2
(and its specialization) could be found.