应该对没有副作用的废值表达式进行诊断吗? [英] Should a diagnostic be emmited for discarded value expressions that do not have side effects?

查看:326
本文介绍了应该对没有副作用的废值表达式进行诊断吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

经过相当多的调试时间后,我觉得愚蠢的发现一个goof在我的代码,归结到这样的:

  int main()
{
double p1 [] = {1,2,3};
double p2 [] = {1,2,3};
int color = 1;
bool some_condition = true;

if(some_condition)(p1,p2,color);
}

(p1,p2,color) code> expression计算到它的最后一个操作,但应该编译器保护我在某种方式? (Visual Studio不告诉任何东西)



是的,你猜到了,我想调用一个绘制函数: Draw(p1,p2,color) c>(p1,p2,color) c>

解决方案

/ code>强制编译器将括号内的逗号解释为顺序求值运算符
顺序求值运算符是一个二进制运算符,它将其第一个操作数计算为 void 并丢弃结果,然后计算第二个操作数并返回其值和类型。因此,表达式(p1,p2,color)将以下列方式计算:


  1. 首先 p1 被计算并丢弃,然后评估(p2,color) (p2,color)

  2. 首先 p2 则会返回 color ,并返回结果 color

因此语句:

  if(some_condition)(p1,p2,color ); 

等效于:

  if(some_condition)color; 

一些编译器可能会引发警告,因为在表达式 ,p2,color) p1 p2 的计算将导致未使用: / p>

CLANG LIVE DEMO
GCC LIVE DEMO
(如您之前提到的 Visual Studio 完全不会产生警告。)



除了这些警告,代码是合法的C ++(即,不违反C ++语法)。



现在,无论编译器是否应该保护你是有争议的。我的愚见认为它应该保护你,因为这样的表达虽然从C ++语法角度来看是正确的可能会导致,很难发现错误(例如,在如果 expression)。


After quite some debugging time I felt silly to discover a goof in my code that boils down to something like this :

int main()
{
    double p1[] = { 1, 2, 3 };
    double p2[] = { 1, 2, 3 };
    int color = 1;
    bool some_condition = true;

    if (some_condition) (p1, p2, color);
}

The (p1, p2, color) expression evaluates to its last operant, but should the compiler protect me in some way ? (Visual Studio told nothing)

And yes you guessed it right, I wanted to call a draw function : Draw(p1, p2, color)

解决方案

In C++ the expression (p1, p2, color) forces the compiler to interpret the commas inside the parentheses as the sequential-evaluation operator. The sequential-evaluation operator is a binary operator that evaluates its first operand as void and discards the result, it then evaluates the second operand and returns its value and type. Therefore, the expression (p1, p2, color) is going to be evaluated in the following way:

  1. First p1 is evaluated and discarded, then (p2, color) is evaluated and the result (p2, color) is returned.
  2. First p2 is evaluated and discarded, then color is evaluated and the result color is returned.

Thus the statement:

if (some_condition) (p1, p2, color);

Is equivalent to:

if (some_condition) color;

Some compilers might raise a warning because during the evaluation of the expression (p1, p2, color) the evaluations of p1 and p2 will result in unused:

CLANG LIVE DEMO GCC LIVE DEMO (As you already mentioned Visual Studio will raise no warning at all.)

Apart from these warnings the code is legitimate C++ (i.e., C++ syntax isn't violated).

Now, whether the compiler should protect you is debatable. In my humble opinion it should protect you, since such expressions although correct from C++ syntax point of view might result, in very hard to spot bugs (e.g., the case of assingment inside an if expression).

这篇关于应该对没有副作用的废值表达式进行诊断吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆