将 c++ 中的 if-else 替换为 ?:
原代码是:
if (expression1) statement1;
else statement2;
能改成这样吗?
expression1 ? statement1 : statement2
或者这取决于编译器?这似乎与 C++ 标准不同?
有时情况是 if (expression1) statements1;
我该如何转换它?
顺便说一句,它可以用 c 完成。
使源代码不可读正是我想要做的
这只是步骤之一
错误:条件运算符的第二个操作数是“void”类型,但第三个操作数既不是抛出表达式,也不是“void”类型
< br> 这是我在编译时使用 g++ (TDM-2 mingw32) 4.4.1 得到的结果
#include <stdio.h>
void _(int __, int ___, int ____, int _____)
{
((___ / __) <= _____) ? _(__,___+_____,____,_____) : !(___ % __) ? _(__,___+_____,___ % __, _____) :
((___ % __)==(___ / __) && !____) ? (printf("%d\n",(___ / __)),
_(__,___+_____,____,_____)) : ((___ % __) > _____ && (___ % __) < (___ / __)) ?
_(__,___+_____,____,_____ + !((___ / __) % (___ % __))) : (___ < __ * __) ?
_(__,___+_____,____,_____) : 0;
}
int main() {
_(100,0,0,1);
return 0;
}
,如果我用 throw 0
替换最后一个 0,它将成功编译。
The original code is:
if (expression1) statement1;
else statement2;
is it possible to transform it into this?
expression1 ? statement1 : statement2
or it depends on the compiler? it seems that this differs within c++ standards?
Sometimes the case is if (expression1) statement1;
and how can i transform that?
btw, it can be done in c.
Making the source code unreadable is exactly what i am trying to do
This is just one of the steps
error: second operand to the conditional operator is of type 'void', but the third operand is neither a throw-expression nor of type 'void'
This is what i got with g++ (TDM-2 mingw32) 4.4.1 when compile
#include <stdio.h>
void _(int __, int ___, int ____, int _____)
{
((___ / __) <= _____) ? _(__,___+_____,____,_____) : !(___ % __) ? _(__,___+_____,___ % __, _____) :
((___ % __)==(___ / __) && !____) ? (printf("%d\n",(___ / __)),
_(__,___+_____,____,_____)) : ((___ % __) > _____ && (___ % __) < (___ / __)) ?
_(__,___+_____,____,_____ + !((___ / __) % (___ % __))) : (___ < __ * __) ?
_(__,___+_____,____,_____) : 0;
}
int main() {
_(100,0,0,1);
return 0;
}
and if i replace the last 0 with throw 0
, it will compile successfully.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
表达式1 ? statements1 : statements2
这实际上是不正确的。正确的是:表达式1 ?表达式2 : 表达式3
并不是任何语句都可以等价地转换为单个表达式,因此通常情况下这并不总是可能的。例如:
您不能将其转换为
?:
表达式,因为for
是一个语句,而不是一个表达式。顺便提一句。它无法在标准 C 中完成。您所指的可能是 语句表达式,它是一个 GCC 扩展。
expression1 ? statement1 : statement2
This is actually incorrect. The correct is this:expression1 ? expression2 : expression3
Not any statement can be equivalently transformed into a single expression, so in general it is not always possible. For example:
You can't transform this into
?:
expression becausefor
is a statement, not an expression.Btw. It can't be done in standard C. What you are referring to is probably the statement expression which is a GCC extension.
转换单个 if
您可以使用 (void(0)) 的技巧来
但我不建议这样做/使用它!
? :
可能会导致意大利面条式代码,并尽量避免它。最好有清晰且易于阅读和理解的代码。此外,两种方式的代码都是相同的,没有特殊原因要编写难以阅读的代码。
You can transform the single if
with a trick of the (void(0)) this ways
but I am not suggest to do it/use it !
the
? :
can lead to spaghetti code and try to avoid it. Its better to have a clear and easy to read and understand code.Also the code is the same on both ways and there is not special reason to make a hard to read code.
为什么?您唯一会影响的人是代码的维护者。如果您想阻止黑客理解您的代码,请不要打扰。他们知道书中的每一个技巧,以及书中没有的一些技巧。
Why? The only people you are going to affect are the maintainers of your code. If you are trying to keep hackers from understanding your code, don't bother. They know every trick in the book, and several that aren't in the book.
代码:
在 C++ 中的工作方式与在 (GCC) C 中的工作方式相同。
代码:
已经很简单了。你为什么想要改变这个?
The code:
works the same in C++ as it does in (GCC) C.
The code:
Is already as simple as things go. Why would you even want to transform this?
除了Armen答案之外,还可以提到C语法将statement定义为以下之一:labeled_statement、compound_statement、express_statement、selection_statement、iteration_statement和jump_statement。在所有这些中,只有表达式语句实际上是表达式。
In addition to Armen answer, it may be mentioned that C grammar defines statement as one of the following: labeled_statement, compound_statement, expression_statement, selection_statement, iteration_statement and jump_statement. Of all of them, only expression statement is, actually, an expression.
与 ?: 问题无关,但我认为用双下划线定义自己的标识符或在全局命名空间中以下划线开头的标识符是不合法的 - 请参阅 这个问题。
Not related to the ?: issue, but I don't think it is legal to define your own identifiers with double underscores, or identifiers beginning with underscores in the global namespace - see this question.
?: 是一个有趣的运算符......实际上它只是一种样式选择,但是使用它并不会损失可读性。
任何时候你有一个简单的 If/Else 块来设置二进制变量,这都是一个很棒的选择。
这可以很容易地转换为 ?三元运算符。
Exp1 计算原始 If 语句中的条件。如果 Exp1 为 (true),则计算 Exp2 并成为语句的值,如果 Exp1 为 (false),则计算 Exp3 并成为语句的值。
如果您使用 ?在 Exp2 和 Exp3 中使用内联函数的三元,行长度可能会变得长得难以阅读,同时允许您执行几乎任何操作,这满足了开头的问题。
大多数情况下,我会保持简短并在保护语句中使用此运算符来制作溢出安全代码。它在视觉上与众不同,我个人认为这极大地增加了可读性。
?: is a fun operator.....really it is just a style choice however, there is no loss in readability to using it.
Any time you have a simple If/Else block setting a binary variable it is a terrific choice.
This can easily be transformed to a ? ternary operator.
Exp1 the condition from the original If statement is evaluated. If Exp1 is (true), Exp2 is evaluated and becomes the value of the statement, if Exp1 is (false) then Exp3 is evaluated and becomes the value of the statement.
If you use the ? ternary with inline functions in Exp2 and Exp3 the line length can get unreadably long while allowing you to do just about anything, which satisfies the opening question.
Mostly I keep it short and use this operator in guard statements to make overrun safe code. It's visually distinctive which I personally feel adds greatly to the readability.