I'm trying to find out whether partial specification of templated functions is part of the C++ standard, or whether this is something compiler specific.
By partial specification, I mean specifying only the types the compiler can't deduce. So if I have a template function 'f' that takes 3 types, and one is used in a parameter and can be deduced, I might call 'f' with the form f<type, type>(parameter)
Here's an example:
#include <iostream>
#include <tuple>
#include <string>
template<class A, class B, class C>
std::tuple<A, B> test(C c)
{
// do something based on c, return tuple with types A and B
return std::make_tuple(A(), B());
}
int main(void)
{
// I expected I would have to use this form. Specify all parameters.
std::tuple<int, int> value3 = test<int, int, int>(5);
// Here, I only specified the return value types, did not specify the parameter type, yet it compiles.
auto value1 = test<int, int>("c-string");
// Similar example here. Return types specified, parameter type deduced. Compiles fine.
auto value2 = test<std::string, int>(42);
return 0;
}
I've tested this with g++ 4.5.3, g++ 4.6.3, VS2010 and VS2012. Since it seems to be widely supported by the compilers, I'm betting it is part of the standard, but can anyone confirm that? Does anyone have any links or pointers to resources that might explain why this works?
Paragraph 14.8.1.2 of the C++03 standard states
"Trailing template arguments that can be deduced (14.8.2) may be omitted from the list of explicit template-arguments."
来源:https://stackoverflow.com/questions/13554496/partial-template-function-specification-in-c-works-but-why