是否可以执行 'assignment by operator' 操作?

Is it possible to cast an 'assignment by operator' operation?

例如:

int a = 10;
float b 1.5;
a*=b; //"warning C4244: '*=' : conversion from 'float' to 'int', possible loss of data"

我想禁止显示此警告。当然,这样做的一个原因是:

a = (int)(a*b);

其实我有两个问题:

  1. 有没有办法继续使用运算符赋值和 把它放在中间?
  2. 有什么方法可以使用强制转换来抑制警告 w/o?

Is there a way to keep using the assignment by operator and casting it in between?

没有。 a *= b 的 RHS 上的任何内容都会影响 b 而不是产品 a*b.

Is there a way I can suppress the warning w/o using casting?

使用最接近的整数函数来处理转换。下面的 2 个函数四舍五入到最接近而不是截断 some_int = some_float.

#include <math.h>

// long int lrintf(float x); 
// round their argument to the nearest integer value
// rounding according to the current rounding direction.
int a = 10;
float b = 1.5;
a = lrintf(a * b);

// or

// long int lroundf(float x);
// The lround and llround functions round their argument to the nearest integer value, 
// rounding halfway cases away from zero, regardless of the current rounding direction.