gpt4 book ai didi

c++ - 为什么小于运算符接受不同类型的参数而 std::min 不接受?

转载 作者:可可西里 更新时间:2023-11-01 17:39:04 26 4
gpt4 key购买 nike

#include <iostream>

int main(){
int a = 1;
long long b = 2;
std::cout<<(a<b);
std::cout<<std::min(a, b);
return 0;
}

> In file included from /usr/include/c++/4.8/bits/char_traits.h:39:0,
> from /usr/include/c++/4.8/ios:40,
> from /usr/include/c++/4.8/ostream:38,
> from /usr/include/c++/4.8/iostream:39,
> from sum_to.cpp:1: /usr/include/c++/4.8/bits/stl_algobase.h:239:5: note: template<class
> _Tp, class _Compare> const _Tp& std::min(const _Tp&, const _Tp&, _Compare)
> min(const _Tp& __a, const _Tp& __b, _Compare __comp)
> ^ /usr/include/c++/4.8/bits/stl_algobase.h:239:5: note: template argument deduction/substitution failed: sum_to.cpp:7:29:
> note: deduced conflicting types for parameter ‘const _Tp’ (‘int’ and
> ‘long long int’)
> std::cout<<std::min(a, b);

---

感谢chris评论 function overloading post 模板参数推导不考虑转换。一 模板参数不能匹配两种类型

所以 std::min失败。

为什么 <有用吗?

最佳答案

因为内置 <适用 Numeric promotions ,而模板参数推导不会。

关于c++ - 为什么小于运算符接受不同类型的参数而 std::min 不接受?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31936418/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com