“x = ++x” is it really undefined?

前端 未结 6 815
悲&欢浪女
悲&欢浪女 2020-12-16 15:50

I am using Coverity Prevent on a project to find errors.

It reports an error for this expression (The variable names are of course changed):

x=
   (a         


        
6条回答
  •  夕颜
    夕颜 (楼主)
    2020-12-16 16:20

    Regardless of the accuracy of the message, replacing the code in question by x= (a>= b) ? x+1: 0; achieves the same end without any confusion. If the tool is confused then maybe the next person to look at this code will be too.

    This does assume that x does not have an overloaded increment operator with side-effects that you rely on here.

提交回复
热议问题