[英]Can I multiply an int with a boolean in C++?
I have a widget in my GUI that displays chart(s). 我的GUI中有一个显示图表的小部件。 If I have more than one chart there will be a legend shown in a rectangle on the GUI. 如果我有多个图表,GUI上的矩形会显示一个图例。
I have a QStringlist (legendText)
which holds the text of the legend. 我有一个QStringlist (legendText)
,它包含图例的文本。 If there is no legend required, legendText
would be empty. 如果不需要图例, legendText
将为空。 If there will be a legend, legendText
would hold the text. 如果有一个图例, legendText
会保存文本。
For finding the height of the rectangle around the legend I would like to do the following: 为了找到图例周围的矩形高度,我想执行以下操作:
int height = 10;
QStringList legendText;
...
height = height * (legendText->size() > 0);
...
Is this a good idea/ good style to multiply an int
with a boolean
? 将int
与boolean
相乘是一个好主意/好的风格吗? Will I run into problems with that? 我会遇到问题吗?
This is technically fine, if a bit unclear. 如果有点不清楚,这在技术上很好。
The bool
will be promoted to an int
, so the result is well-defined. bool
将被提升为 int
,因此结果是明确定义的。 However, looking at that code I don't instantly get the semantics you are trying to achieve. 但是,查看该代码我不会立即获得您想要实现的语义。
I would simply write something like: 我只想写下这样的东西:
height = legendText->isEmpty() ? 0 : height;
This makes your intent far clearer. 这使您的意图更加清晰。
It's perfectly fine according to the standard (§4.5/6): 根据标准(§4.5/ 6),它完全没问题:
A prvalue of type
bool
can be converted to a prvalue of typeint
, withfalse
becoming zero andtrue
becoming one.bool
类型的prvalue可以转换为int
类型的prvalue,false
变为零,true
变为1。
However, I suggest using isEmpty
instead of comparing size
to zero height = height * (!legendText->isEmpty());
但是,我建议使用isEmpty
而不是将size
与zero height = height * (!legendText->isEmpty());
Or use the conditional operator as the other answers suggest (but still with isEmpty
instead of .size() > 0
) 或者使用条件运算符作为其他答案建议(但仍使用isEmpty
而不是.size() > 0
)
您可以使用条件(三元)运算符:
height = ( legendText->size() >0 ) ? height : 0 ;
Maybe this? 也许这个?
if(legendText->isEmpty())
{
height = 0;
}
or 要么
int height = legendText->isEmpty() ? 0 : 10;
Some people may find following information useful (following code should be considered in high performance programs where every clock cycle matters and it's purpose here is to show alternative techniques, I wouldn't use it in this particular situation). 有些人可能会发现以下信息很有用(下面的代码应该在每个时钟周期都很重要的高性能程序中考虑,这里的目的是展示替代技术,我不会在这种特殊情况下使用它)。
If you need fast code without branches you can implement int multiplication with boolean using bitwise operators. 如果你需要没有分支的快速代码,你可以使用按位运算符实现int乘法与布尔值。
bool b = true;
int number = 10;
number = b*number;
can be optimized to: 可以优化为:
number = (-b & number);
If b
is true
then -b
is -1
and all bits are set to 1
. 如果b
为true
则-b
为-1
,所有位都设置为1
。 Otherwise all bits are 0
. 否则所有位都为0
。
Boolean NOT ( !b
) can be implemented by XOR'ing b
with 1
( b^1
). 布尔NOT( !b
)可以通过XOR'ing b
以1
( b^1
)来实现。
So in your case we get following expression: 所以在你的情况下我们得到以下表达式:
height = (-(legendText->isEmpty()^1) & height);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.