#include <iostream>
int main()
{
unsignedshort width { 5 };
unsignedshort length { 10 };
// the following errors out when using uniform initialization
unsignedshort area { width * length };
std::cout << "width: " << width << "\n";
std::cout << "length: " << length << "\n";
std::cout << "area: " << area << "\n";
}
error in VS2019 and Code::Blocks. VS error:
Error C2397 conversion from 'int' to 'unsigned short' requires a narrowing conversion Project1 D:\Programming\Projects 2019\cplusplus2019\Project1\Source1.cpp 9
unsigned char produces the same error.
Declare the variables as signed short or char, and no problems with narrowing. Same for ints, longs or long longs, signed or unsigned.
Weird. Would this be a defect in VS/CB, or something not addressed in the standard?
All the binary operators convert their operands to ints or bigger, except right- and left- bitshifts.
Each operand undergoes integral promotion, performed as part of the usual arithmetic conversions. The type of the expression width * length is int, which won't narrow to unsigned short inside braces.
#include <iostream>
int main()
{
short width { 5 };
short length { 10 };
short area { width * length };
std::cout << "width: " << width << "\n";
std::cout << "length: " << length << "\n";
std::cout << "area: " << area << "\n";
}
No warning or error from VS2019. Code::Blocks spits out a warning, only when doing a full rebuild:
D:\Programming\Projects\CBTest\main.cpp|8|warning: narrowing conversion of '(((int)width) * ((int)length))' from 'int' to 'short unsigned int' inside { } [-Wnarrowing]|
mbozzi wrote:
All the binary operators convert their operands to ints or bigger, except right- and left- bitshifts.
Each operand undergoes integral promotion, performed as part of the usual arithmetic conversions. The type of the expression width * length is int, which won't narrow to unsigned short inside braces.
Now THAT makes sense, in conjunction with the warning from C::B. VS is not completely following the standard. Weird that unsigned short/char gives VS heartburn, but not (signed) short/char.