Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why less than operator accepts different types of params while std::min not?

#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);

---

Thanks to chris comment in function overloading post Template argument deduction doesn't take conversions into account. One template parameter can't match two types

So std::min fail.

Why < would work?

like image 942
Kamel Avatar asked Aug 11 '15 08:08

Kamel


People also ask

What does std :: min do?

std::min in C++ std::min is defined in the header file <algorithm> and is used to find out the smallest of the number passed to it. It returns the first of them, if there are more than one.

Which library is min in C++?

C++ valarray Library - Function min.


1 Answers

Because built-in < applies Numeric promotions, and template argument deduction doesn't.

like image 66
Quentin Avatar answered Oct 18 '22 14:10

Quentin