3

i have a short program, that is not compiling:

    #include <algorithm>

    int main(int argc, char *argv[])
    {
        unsigned short a = 490;
        unsigned short b = 43;

        unsigned short d = std::min(b, a-b);

        return 0;
    }

the compiler (g++ version 4.8.4) says:

    ./main.cpp: In function ‘int main(int, char**)’:
    ./main.cpp:25:47: error: no matching function for call to ‘min(short unsigned int&, int)’
                 unsigned short d = std::min(b, a-b);
                                                   ^
    ./main.cpp:25:47: note: candidates are:
    In file included from /usr/include/c++/4.8/algorithm:61:0,
                     from ./main.cpp:18:
    /usr/include/c++/4.8/bits/stl_algobase.h:193:5: note: template<class _Tp> const _Tp& std::min(const _Tp&, const _Tp&)
         min(const _Tp& __a, const _Tp& __b)
         ^
    /usr/include/c++/4.8/bits/stl_algobase.h:193:5: note:   template argument deduction/substitution failed:
    ./main.cpp:25:47: note:   deduced conflicting types for parameter ‘const _Tp’ (‘short unsigned int’ and ‘int’)
                 unsigned short d = std::min(b, a-b);
                                                   ^
    In file included from /usr/include/c++/4.8/algorithm:61:0,
                     from ./main.cpp:18:
    /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:
    ./main.cpp:25:47: note:   deduced conflicting types for parameter ‘const _Tp’ (‘short unsigned int’ and ‘int’)
                 unsigned short d = std::min(b, a-b);

so apparently the unsigned short subtraction results in an integer.

Question is: Why?

Edit: I saw the duplicate when searching, but didn't read to the end. They say: "Note. The minimum size of operations is int. So short/char are promoted to int before the operation is done." Which explains the behaviour..

Adam
  • 488
  • 4
  • 17
  • 1
    @AlanStokes I do not think that duplicate answers this question. In the OPs example, both types are `unsigned int`, so it is not that one operand is being promoted to the other. – Cory Kramer Apr 24 '16 at 15:41
  • 1
    @CoryKramer `a` and `b` are both `unsigned short`, and so get promoted to `int` for the subtraction. Which is why the compiler is then looking for `min(unsigned short int, int)`. – Alan Stokes Apr 24 '16 at 15:42
  • @Alan Stokes why are they promoted to int for subtraction. they are both `unsigned short`? I read on one of the answers from the duplicate link stating "*If both operands have the same type, no further conversion is needed*". – Biruk Abebe Apr 24 '16 at 15:50
  • @AlanStokes I think that is the point of confusion though. The duplicate link describes that when two operands differ in type, one is promoted to the other. But if the two operands are of the same type, why is any promotion required? http://en.cppreference.com/w/cpp/language/operator_arithmetic – Cory Kramer Apr 24 '16 at 15:50
  • 1
    @Adam To more directly answer your question, [from here](http://en.cppreference.com/w/cpp/language/implicit_conversion#integral_promotion) *"In particular, arithmetic operators do not accept types smaller than `int` as arguments, and integral promotions are automatically applied after lvalue-to-rvalue conversion, if applicable. This conversion always preserves the value."* – Cory Kramer Apr 24 '16 at 15:53
  • 2
    @CoryKramer Because types smaller than `int` are always promoted. The top answer says "Note. The minimum size of operations is int. So short/char are promoted to int before the operation is done.". See also http://en.cppreference.com/w/cpp/language/implicit_conversion (under "integral promotion"). – Alan Stokes Apr 24 '16 at 15:54
  • @AlanStokes I didn't notice that the answer in the duplicate post mentioned that, good catch, my mistake. I was not saying that you were incorrect, integral promotion rules do apply (as I linked above) I just didn't realize that the duplicate post touched on that detail, and was looking for another link that described that behavior. – Cory Kramer Apr 24 '16 at 15:56
  • @CoryKramer Yeah, that answer could maybe do with some light editing. I couldn't find a better canonical answer. – Alan Stokes Apr 24 '16 at 15:59

0 Answers0