c++ - 是否可以在调用函数模板时只指定一些模板参数,然后让编译器推导其他参数

标签 c++ templates type-deduction function-templates

考虑以下函数模板:

template <typename T1, typename T2>
void foo(T1 a, T2 b){// Do something.}

现在假设我想要 T1成为int ,但是T2由编译器根据 b 的类型推断。类似 foo<int,...>(1,b) 。可能吗?

谢谢!

最佳答案

是的!

foo<int>(1, b);

但在上面的例子中没有任何好处。如果您的第一个参数尚未被推导为 int,则差异是可见的:

foo<int>(3.2f, b);
//       ^^^^ Implicit conversion

关于c++ - 是否可以在调用函数模板时只指定一些模板参数,然后让编译器推导其他参数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60392060/

相关文章:

c++ - 将类型转换为整数的元函数,反之亦然

c++ - 编译器错误 `assigning incompatible type within if statement`

c++ - 将两种不同类型的模板定义专门化

C++将值添加到一种矩阵

templates - 防止模板中转义正斜杠

c++ - auto* 的类型推导规则是什么?

c++ - 在扣除 auto 之前使用 decltype(auto) <func>

c++ - 为什么迭代器类型推导会失败?

c++ - POSIX pthread 多次使用同一个线程

c++ - 当有指向其中其他对象的指针时创建对象数组