Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there an equivalent of Python's `pass` in c++ std11?

Tags:

c++

python

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

Edit: Just saw: How does one execute a no-op in C/C++?

#define pass (void)0 

Solved my problem. Thanks!

like image 626
Tommy Avatar asked Dec 04 '13 17:12

Tommy


People also ask

Is there a pass command in C?

Parameters in C functions There are two ways to pass parameters in C: Pass by Value, Pass by Reference.

What is pass equivalent in C?

and class C: pass. becomes class C {};

Is there any pass statement in C++?

No, you just use an empty block like: void function(int parameter) {

What is the C++ equivalent of Python's in Operator?

In C++ you can use std::find to determine whether or not an item is contained in a std::vector .


2 Answers

A null statement (just Semicolon), or empty brackets should work for you

For example Python's

while some_condition():    # presumably one that eventually turns false     pass 

Could translate to the following C++

while (/* some condition */)     ; 

Or

while (/* some condition */) {} 

Perhaps for the ternary operator case, you could do:

x > y ? do_something() : true; 
like image 119
Prashant Kumar Avatar answered Oct 05 '22 14:10

Prashant Kumar


No. You don't have pass or equivalent keyword. But you can write equivalent code without any such keyword.

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 using to inherits constructors from the base class. In Python, pass does the similar thing, in similar context.

Hope that helps.

like image 22
Nawaz Avatar answered Oct 05 '22 15:10

Nawaz