为什么不能在 C++ 中比较 int 和 size_t
Why can't compare int and size_t in c++
问题是如果我在 for 循环中比较 int 和 size_t 它工作正常。
vector<int> v;
for (int i = 0; i < v.size(); ++i)
但是,如果我这样做是行不通的:
vector<int> v;
int max_num = max(3, v.size());
Line 13: Char 24: error: no matching function for call to 'max'
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/algorithmfwd.h:370:5: note: candidate template ignored: deduced conflicting types for parameter '_Tp' ('int' vs. 'unsigned long')
max(const _Tp&, const _Tp&);
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_algo.h:3462:5: note: candidate template ignored: could not match 'initializer_list<type-parameter-0-0>' against 'int'
max(initializer_list<_Tp> __l, _Compare __comp)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_algo.h:3456:5: note: candidate function template not viable: requires single argument '__l', but 2 arguments were provided
max(initializer_list<_Tp> __l)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/algorithmfwd.h:375:5: note: candidate function template not viable: requires 3 arguments, but 2 were provided
max(const _Tp&, const _Tp&, _Compare);
^
1 error generated.
在最底下的情况下,我必须将 size_t 转换为 int 才能使其工作。
为什么在 for 循环中我不需要这样做?只是好奇。
在 C++ 中,当二元运算符的类型不同时,有各种(复杂的)规则来管理二元运算符使用的实际类型:
i < v.size()
其中之一是 int
。另一个是size_t
。没关系,有一些规则可以确定用于比较的实际类型(int
值被类型转换为 size_t
,然后 <
运算符进行比较。
max(3, v.size());
std::max
的定义在功能上要求它的两个参数是同一类型。他们不是。因此编译错误。
既然您了解了原因,请尝试重新阅读以下错误消息,看看您现在是否理解编译器告诉您的确切原因:
deduced conflicting types for parameter '_Tp' ('int' vs. 'unsigned long') max(const _Tp&, const _Tp&);
正如您在此处看到的,编译器正在非常努力地消化具有两个参数的函数,这两个参数的类型相同:const _Tp &
。 max()
的定义是这样写的:它的两个参数都是同一类型。
好吧,可怜的编译器看到这些参数之一是 int
,另一个是 size_t
(a.k.a. "unsigned long") 并放弃了。
问题是如果我在 for 循环中比较 int 和 size_t 它工作正常。
vector<int> v;
for (int i = 0; i < v.size(); ++i)
但是,如果我这样做是行不通的:
vector<int> v;
int max_num = max(3, v.size());
Line 13: Char 24: error: no matching function for call to 'max'
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/algorithmfwd.h:370:5: note: candidate template ignored: deduced conflicting types for parameter '_Tp' ('int' vs. 'unsigned long')
max(const _Tp&, const _Tp&);
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_algo.h:3462:5: note: candidate template ignored: could not match 'initializer_list<type-parameter-0-0>' against 'int'
max(initializer_list<_Tp> __l, _Compare __comp)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/stl_algo.h:3456:5: note: candidate function template not viable: requires single argument '__l', but 2 arguments were provided
max(initializer_list<_Tp> __l)
^
/usr/bin/../lib/gcc/x86_64-linux-gnu/9/../../../../include/c++/9/bits/algorithmfwd.h:375:5: note: candidate function template not viable: requires 3 arguments, but 2 were provided
max(const _Tp&, const _Tp&, _Compare);
^
1 error generated.
在最底下的情况下,我必须将 size_t 转换为 int 才能使其工作。
为什么在 for 循环中我不需要这样做?只是好奇。
在 C++ 中,当二元运算符的类型不同时,有各种(复杂的)规则来管理二元运算符使用的实际类型:
i < v.size()
其中之一是 int
。另一个是size_t
。没关系,有一些规则可以确定用于比较的实际类型(int
值被类型转换为 size_t
,然后 <
运算符进行比较。
max(3, v.size());
std::max
的定义在功能上要求它的两个参数是同一类型。他们不是。因此编译错误。
既然您了解了原因,请尝试重新阅读以下错误消息,看看您现在是否理解编译器告诉您的确切原因:
deduced conflicting types for parameter '_Tp' ('int' vs. 'unsigned long') max(const _Tp&, const _Tp&);
正如您在此处看到的,编译器正在非常努力地消化具有两个参数的函数,这两个参数的类型相同:const _Tp &
。 max()
的定义是这样写的:它的两个参数都是同一类型。
好吧,可怜的编译器看到这些参数之一是 int
,另一个是 size_t
(a.k.a. "unsigned long") 并放弃了。