Conside the following code:
int main()
{
signed char a = 10;
a += a; // Line 5
a = a + a;
return 0;
}
I am getting this warning at Line 5:
d:\codes\operator cast\operator
cast\test.cpp(5) : warning C4244: ‘+=’
: conversion from ‘int’ to ‘signed
char’, possible loss of data
Does this mean that += operator makes an implicit cast of the right hand operator to int?
P.S: I am using Visual studio 2005
Edit: This issue occurs only when the warning level is set to 4
What you are seeing is the result of integral promotion.
Integral promotion is applied to both arguments to most binary expressions involving integer types. This means that anything of integer type that is narrower than an
intis promoted to anint(or possiblyunsigned int) before the operation is performed.This means that
a += ais performed as anintcalculation but because the result is stored back intoawhich is acharthe result has to undergo a narrowing conversion, hence the warning.