The subject looks a bit confusing, however I don't know how to formulate it more properly, sorry =)
Let's look a the following code
#include <iostream>
template<typename T>
void f(T value) {
std::cout << "f<T>" << std::endl;
}
template<>
void f(int value) {
std::cout << "f<int>" << std::endl;
}
template<typename T>
struct S {
using type = T;
};
template<typename T>
void f(typename S<T>::type value) {
std::cout << "f<S<T>>" << std::endl;
};
int main() {
f(123);
f<int>(123);
}
The output is
$ ./testgcc
f<int>
f<S<T>>
So the question is why the first call results in f<int>
specialization and the second with explicit int template argument results in call to "templated" f<S<int>>()
? Is there a rule in standard which states how to instantiate templates in such situations?
Thanks in advance!
PS Tested with different versions of gcc and clang - the behavior is the same. I don't have windows system to test with MSVC, however I tested at godbolt and MSVC results int the following code:
_main PROC
; ....
push 123 ; 0000007bH
call void f<int>(int) ; f<int>
add esp, 4
push 123 ; 0000007bH
call void f<int>(int) ; f<int>
; ...
So MSVC calls f<int>
in both cases. Is this behavior documented as implementation defined?