小编Jam*_*man的帖子

C++中的部分模板函数规范有效,但为什么呢?

我试图找出模板化函数的部分规范是否是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. …
Run Code Online (Sandbox Code Playgroud)

c++ templates function-templates template-argument-deduction

14
推荐指数
1
解决办法
1668
查看次数