[英]How does one execute a no-op in C/C++?
for the following: 对于以下内容:
( a != b ) ? cout<<"not equal" : cout<<"equal";
suppose I don't care if it's equal, how can I use the above statement by substituting cout<<"equal"
with a no-op. 假设我不在乎它是否相等,我如何通过用无操作替换cout<<"equal"
来使用上述语句。
If it really is for a ternary operator that doesn't need a second action, the best option would be to replace it for an if: 如果它确实适用于不需要第二个操作的三元运算符,那么最好的选择是将其替换为if:
if (a!=b) cout << "not equal";
it will smell a lot less. 它的味道会少很多。
Simple: I would code it as 简单:我会将其编码为
if (a != b)
cout << "not equal";
The ternary operator requires the two results to be of the same type. 三元运算符要求两个结果属于同一类型。 So you might also be able to get away with 所以你也可以逃脱
(a != b) ? cout << "not equal" : cout;
because the stream operator (<<) just returns the ostream reference. 因为流运算符(<<)只返回ostream引用。 That's ugly and unnecessary in my opinion though. 在我看来,这是丑陋和不必要的。
The only thing missing from the other answers is this: There is no way, directly, to code a "noop" in C/C++. 其他答案中唯一缺少的是:在C / C ++中没有办法直接编写“noop”代码。
Also, doing: (a != b) ? : printf("equal\\n");
另外,做: (a != b) ? : printf("equal\\n");
(a != b) ? : printf("equal\\n");
does actually compile for me (gcc -ansi in gcc 4.2.4). 确实为我编译(gcc 4.2.4中的gcc -ansi)。
(void)0;
is noop. 是noop。 There is a lots of good reason to use expr?false:true
form. 有很多很好的理由使用expr?false:true
form。 Look how assert() is implemented. 看看assert()是如何实现的。
So in your example, use ( a != b ) ? (void)0 : cout<<"equal";
所以在你的例子中,使用( a != b ) ? (void)0 : cout<<"equal";
( a != b ) ? (void)0 : cout<<"equal";
In C++11 you can write ( in case of void ) : 在C ++ 11中,您可以编写(如果无效):
somecondition ? foo() : [] {} () ;
So the NOP is actually an empty lambda. 所以NOP实际上是一个空的lambda。 Besides void you could return any type and value. 除了void,你可以返回任何类型和价值。
This might look a bit overkill all by itself but suppose you have this : 这可能看起来有点矫枉过正,但假设你有这个:
somecondition1 ? foo1() :
somecondition2 ? foo2() :
somecondition3 ? foo3() :
flip_out_because_unhandled_condition() ;
Now if someone adds somecondition4, but forgets to include it in the handling code, the software will call the flip_out_... function causing all kinds of unwanted effects. 现在,如果有人添加了somecondition4,但忘记将其包含在处理代码中,软件将调用flip_out _...函数,从而导致各种不需要的影响。 But maybe somecondition4 doesn't need any special attention, it just needs to be ignored. 但也许somecondition4不需要特别注意,只需要忽略它。 Well then you could write : 那么你可以写:
somecondition1 ? foo1() :
somecondition2 ? foo2() :
somecondition3 ? foo3() :
somecondition4 ? []{}() :
flip_out_because_unhandled_condition() ;
This is very confusing code. 这是非常令人困惑的代码。 You could just write 你可以写
cond ? cout << "equal" : cout;
but you won't (will you?) because you've got conventional if
for that. 但你不会(你愿意吗?),因为你已经有了常规if
为这一点。
I think the problem here is that the operator : has two EXPRESSIONS as arguments. 我认为这里的问题是运算符:有两个EXPRESSIONS作为参数。 Let's say.. a = x ? 让我们说.. a = x? y : z; y:z;
Expression by definition must have a value...that's why you cannot just "skip". 根据定义,表达式必须具有值...这就是为什么你不能只是“跳过”。
If the focus of the code is the output operation and not the condition, then something like this could be done: 如果代码的焦点是输出操作而不是条件,那么可以这样做:
cout << (cond ? "not equal" : "");
I suspect that's not the case, though, because you want to do nothing in the "else" clause. 我怀疑情况并非如此,因为你想在“else”条款中什么也不做。
if (a!=b) cout<<"not equal";
The syntax just requires a expression. 语法只需要一个表达式。 You can just go: (a!=b)?cout<<"not equal":1; 你可以去:(a!= b)?cout <<“not equal”:1;
Both statements compile: 两个语句都编译:
( a != b ) ? cout<<"not equal" : NULL;
( a != b ) ? NULL : cout<<"equal";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.