Linked Questions

Popular Questions

Integral promotion and operator+=

Asked by At

I need to eliminate gcc -Wconversion warnings. For example

typedef unsigned short     uint16_t;

uint16_t a = 1;
uint16_t b = 2;
b += a;

gives

warning: conversion to 'uint16_t {aka short unsigned int}' from 'int' may alter its value [-Wconversion]
     b += a;
     ~~^~~~

I can eliminate this by

uint16_t a = 1;
uint16_t b = 2;
b = static_cast<uint16_t>(b + a);

Is there any way to keep the operator+= and eliminate the warning? Thank you.

EDIT

I use

gcc test.cpp -Wconversion

my gcc version is

gcc.exe (Rev3, Built by MSYS2 project) 7.2.0

Related Questions