39

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!

Tommy
  • 12,588
  • 14
  • 59
  • 110
  • Possible duplicate, or at least related: http://stackoverflow.com/questions/7978620/whats-a-portable-way-to-implement-no-op-statement-in-c – Peter DeGlopper Dec 04 '13 at 17:47
  • Take a look at this: http://stackoverflow.com/questions/7978620/whats-a-portable-way-to-implement-no-op-statement-in-c – Prashant Kumar Dec 04 '13 at 17:56
  • 4
    The problem is your abuse of the conditional operator as an `if`. The conditional is an expression that yields a value, not a shorthand for an `if` you will have many different problems with code similar to that in C++: `x > y ? getInt() : getString()` for example. Rather than translating your code, you should learn the right idioms – David Rodríguez - dribeas Dec 04 '13 at 17:56
  • 4
    how do you use pass in ternary statements(its an error in python also) and more importantly why would you do this instead of just `if (condition) do_something();` – Joran Beasley Dec 04 '13 at 17:57
  • 1
    use a real placeholder like `do_something_else()` – Joran Beasley Dec 04 '13 at 18:04
  • 2
    @Tommy: No, people like to point out that this is not a good enough excuse. – Lightness Races in Orbit Dec 04 '13 at 18:04
  • 1
    @Tommy: "the tertiary (sic.) can be used as short for" - only when the types of the two expressions are suitable. A proper `if` statement is much less restrictive as well as (arguably) more readable. Why not just use the right construct, rather than jumping through syntactic hoops to save typing a few characters? – Mike Seymour Dec 04 '13 at 18:05
  • 1
    so all the passes will later do the same thing ? this still has a bad code smell to me ... but meh you probably know what your project needs... but I cant imagine this is the right solution.. and I still dont understand how you were doing it in python since `do_soomething() if condition else pass` is not valid python ... – Joran Beasley Dec 04 '13 at 18:07
  • 1
    @Tommy: I don't care that some ad-infested website calls it that, any more than I care that you do! Neither the C nor the C++ standard does. Neither does common sense. – Lightness Races in Orbit Dec 04 '13 at 18:16
  • 2
    @Tommy: cplusplus.com is not offical, not authoritative, and not even particularly well-respected in the C++ community. Furthermore, those forum posts are written by complete randomers. (In fact, that post says nothing about using the conditional operator in the way you're trying to use it.) You can cite random opinions if you like, but the experts here are telling you, with peer-review, that those opinions are wrong. Perhaps it would pay to heed that advice. – Lightness Races in Orbit Dec 04 '13 at 18:19
  • 1
    possible duplicate of [How does one execute a no-op in C/C++?](http://stackoverflow.com/questions/300208/how-does-one-execute-a-no-op-in-c-c) – user Mar 10 '14 at 19:31

7 Answers7

53

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;
Yonas Kassa
  • 3,362
  • 1
  • 18
  • 27
Prashant Kumar
  • 20,069
  • 14
  • 47
  • 63
13

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.

Nawaz
  • 353,942
  • 115
  • 666
  • 851
5

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 broken where C++ is not. As it happens, Python doesn't actually support it either, after all.

Write an if statement, instead:

if (x > y) {
   do_something();
}
else {
   /* Unimplemented at the moment */
}
Lightness Races in Orbit
  • 378,754
  • 76
  • 643
  • 1,055
  • It may be valid, but it's still a bad idea. – Ian McLaird Dec 04 '13 at 18:13
  • 1
    Yes, it's valid. That doesn't make it a good idea, nor the intended use for the operator. You seem to be stubbornly insisting upon writing unclear, hard-to-maintain code. – Lightness Races in Orbit Dec 04 '13 at 18:15
  • @Tommy: Unfortunately you have not paid me enough to write an academic paper on the merits of code style and why this is the case. – Lightness Races in Orbit Dec 04 '13 at 18:15
  • 3
    In a nutshell, the intended semantics are different. The `if`/`else` block conveys an intent to choose between two *actions*, where the conditional operator conveys an intent to choose between two *values*. This is why the conditional operator returns a value, while the `if`/`else` block does not. – Ian McLaird Dec 04 '13 at 18:20
  • @Tommy: If you're attempting to suggest that I'm wrong because I'm unwilling to spend a few hours teaching you the basics of common sense program design, then you ought to become aware that this is a logical fallacy. My answer already summarises what is wrong with your approach. Why are you ignoring it? – Lightness Races in Orbit Dec 04 '13 at 18:21
  • 1
    @Tommy: I wasn't trying to defend anything. I was stating a fact. And I already said what Ian said in my answer: `This is not a "do something" construct, but a "give me one of two things" construct.` – Lightness Races in Orbit Dec 04 '13 at 20:14
  • 1
    pythons not broken it wont let you put a pass in a _ternary_ statement either – Joran Beasley Dec 04 '13 at 20:22
4

I know it's too absurd but you may think using "nop" instruction.

In Linux

void pass()
{
    __asm__("nop");
}

In Windows

void pass()
{
    __asm{nop};
}
ibrakap
  • 41
  • 1
3

I think in C++ just an empty line (;) will be the equivalent of 'pass'

bboonn
  • 31
  • 1
3

As @bboonn suggests:

 if (some_flag)
   ; // Do nothing

 else if (some_other_flag)
   do_something();
slaughter98
  • 1,759
  • 14
  • 20
0
void f() { ; }
void g() { }
void h() { __asm__("nop"); }

all result in almost identical assembly output (x86 64).

f & g both give

push    rbp
mov     rbp, rsp
nop
pop     rbp
ret

while g gives

push    rbp
mov     rbp, rsp
nop
nop
pop     rbp
ret

(one extra nop)

In my testing, it seems that a loop containing just __asm__("nop") takes ~50% longer than ;

When using the -O1 flag or above, the same still applies - f & g become

ret

and h becomes

nop
ret

When using a loop, e.g. for(;;){}, the __asm__ still adds one extra instruction, which is understandable

Doot
  • 555
  • 5
  • 15