Jam*_*man 14 c++ templates function-templates template-argument-deduction
我试图找出模板化函数的部分规范是否是C++标准的一部分,或者这是否是编译器特定的.
通过部分规范,我的意思是仅指定编译器无法推断的类型.所以,如果我有一个模板函数'f',它有三种类型,一个在参数中使用并且可以推导出来,我可以用表格调用'f'f<type, type>(parameter)
这是一个例子:
#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;
}
Run Code Online (Sandbox Code Playgroud)
我用g ++ 4.5.3,g ++ 4.6.3,VS2010和VS2012进行了测试.由于它似乎得到了编译器的广泛支持,我认为它是标准的一部分,但任何人都可以证实这一点吗?有没有人有任何链接或指向资源,可以解释为什么这有效?