Braced-init-lists and function template type deduction order
我有一个关于函数模板参数类型推导程序的问题。
举个例子:
1
2 3 4 5 6 7 8 9 10 11 12 |
#include <vector> #include <sstream> #include <string> #include <iterator> #include <fstream> int main() |
如果我理解正确,第二个参数被推断为
适当的
1
2 3 |
template <class InputIterator>
vector (InputIterator first, InputIterator last, const allocator_type& alloc = allocator_type()); |
由于第一个参数类型被推导出为
提前致谢!
让我们用一个更简单的例子:
1
2 3 4 |
template<class T> void foo(T, T); foo(42, {}); |
函数调用有两个参数:
-
int 类型的纯右值表达式(整数文字) -
一个花括号初始化列表
{}
后者,
对每个函数参数单独进行模板类型扣除[temp.deduct.type]/2。 [temp.deduct.call]/1 说明函数参数
的类型推导
If removing references and cv-qualifiers from
P gives
std::initializer_list< P’> for some P’ and the argument is an
initializer list, then deduction is performed instead for each element
of the initializer list, taking P’ as a function template parameter
type and the initializer element as its argument. Otherwise, an
initializer list argument causes the parameter to be considered a
non-deduced context. [emphasis mine]
所以在调用
一般来说,我们可以从多个函数参数中推导出
推演后,
的函数签名
1
|
void foo<int>(int, int);
|
可以使用两个参数
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/tech/268940.html