this is much like this question Why must a short be converted to an int before arithmetic operations in C and C++? however there is a sub-question why the compiler diagnoses a warning in one case and an error in the other case ,with the exact same expression.
I really like the use of the auto
'type' as in auto var =
..., but MSVC 2015 CTP gives an error from my code.
The issue is I am auto
-ing an expression of type short
but sometimes it's promoted to int
.
Here's a MCVE:
struct MY_COORD { short X; short Y; };
using t_crd = MY_COORD;
void call_test ( t_crd x ) {}
int main()
{
t_crd crd { 10 ,20 };
auto x5 = crd.X - crd.Y;
auto y5 = crd.Y - crd.X;
t_crd crd5 { x5 ,y5 }; // (1)
call_test( t_crd{ x5 ,y5 } ); // (2)
}
The messages from lines (1) and (2) are respectively:
warning C4838: conversion from 'int' to 'short' requires a narrowing conversion
error C2397: conversion from 'int' to 'short' requires a narrowing conversion
I thought this code was OK but it's not according to MSVC 2015 CTP compiler (but Intellisense doesn't indicate it). Is there some obscure little rule that I missed that makes MSVC right ?
And if so why is the same expression a warning in one case and an error in the other case ?
I wanted to use it in the initialization of a loop variable like this:
MY_COORD pos = ResultFromFunction();
auto rows = pos.Y;
for ( auto i = (rows - rows); i < rows; ++i )
{
coord c{ 0 ,i };
...
}