c++ std11 中是否有等效于 Python 的“pass”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20382278/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is there an equivalent of Python's `pass` in c++ std11?
提问by Tommy
I want a statement that does nothing but can be used in places requiring a statement. Pass: http://docs.python.org/release/2.5.2/ref/pass.html
我想要一个什么都不做但可以在需要声明的地方使用的声明。通过:http: //docs.python.org/release/2.5.2/ref/pass.html
Edit: Just saw: How does one execute a no-op in C/C++?
编辑:刚刚看到:如何在 C/C++ 中执行无操作?
#define pass (void)0
Solved my problem. Thanks!
解决了我的问题。谢谢!
回答by Prashant Kumar
Semicolon, or empty brackets should work for you
分号或空括号应该适合你
For example Python's
例如 Python 的
while some_condition(): # presumably one that eventually turns false
pass
Could translate to the following C++
可以翻译成以下 C++
while (/* some condition */)
;
Or
或者
while (/* some condition */) {}
Perhaps for the ternary operator case, you could do:
也许对于三元运算符的情况,您可以这样做:
x > y ? do_something() : true;
回答by Nawaz
No. You don't have passor equivalent keyword. But you can write equivalent code without any such keyword.
不。您没有pass或等效的关键字。但是您可以在没有任何此类关键字的情况下编写等效代码。
def f():
pass
becomes
变成
void f() {}
and
和
class C:
pass
becomes
变成
class C {};
In different context, different syntax could be useful. For example,
在不同的上下文中,不同的语法可能有用。例如,
class MyError(Exception):
pass
becomes
变成
class MyError : public std::exception
{
using std::exception::exception; //inherits constructor!
};
As you can see, in this context, you've to write usingto inherits constructors from the base class. In Python, passdoes the similar thing, in similar context.
如您所见,在此上下文中,您必须编写using从基类继承的构造函数。在 Pythonpass中,在类似的上下文中做类似的事情。
Hope that helps.
希望有帮助。
回答by bboonn
I think in C++ just an empty line (;) will be the equivalent of 'pass'
我认为在 C++ 中只是一个空行 (;) 将等同于“pass”
回答by Lightness Races in Orbit
As has been stated in the comments, this is not supported because it makes no sense. The conditional operator is designed to evaluate to one of two operands. Two. Not one.
正如评论中所述,这是不支持的,因为它没有意义。条件运算符旨在评估两个操作数之一。二。不是一个。
It is not okay to abuse the operator to perform some conditional action in only one of those cases. In fact, it is best that neither operand have any side-effects whatsoever. This is not a "do something" construct, but a "give me one of two things" construct.
仅在其中一种情况下滥用运算符执行某些有条件的操作是不行的。事实上,最好两个操作数都没有任何副作用。这不是“做某事”的构造,而是“给我两件事之一”的构造。
In this regard, if Python were to support what you say it supports, then it would be brokenwhere C++ is not. As it happens, Python doesn't actually support it either, after all.
在这方面,如果 Python 支持您所说的支持,那么C++ 不支持的地方就会被破坏。碰巧的是,Python 实际上也不支持它,毕竟。
Write an ifstatement, instead:
写一个if声明,而不是:
if (x > y) {
do_something();
}
else {
/* Unimplemented at the moment */
}
回答by slaughter98
As @bboonn suggests:
正如@bboonn 建议的那样:
if (some_flag)
; // Do nothing
else if (some_other_flag)
do_something();
回答by ibrakap
I know it's too absurd but you may think using "nop" instruction.
我知道这太荒谬了,但您可能会认为使用“nop”指令。
In Linux
在 Linux 中
void pass()
{
__asm__("nop");
}
In Windows
在 Windows 中
void pass()
{
__asm{nop};
}

