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!

11
  • 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 Dec 4, 2013 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(); Dec 4, 2013 at 17:57
  • 1
    use a real placeholder like do_something_else() Dec 4, 2013 at 18:04
  • 2
    @Tommy: No, people like to point out that this is not a good enough excuse. Dec 4, 2013 at 18:04
  • 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. Dec 4, 2013 at 18:19

7 Answers 7

54

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;
7
  • 2
    For reference, the first is called a null statement.
    – chris
    Dec 4, 2013 at 17:47
  • How was I supposed to know you were thinking about a tertiary? Context matters, both in SO question-asking and C++. Dec 4, 2013 at 17:50
  • @Tommy: that begs the question of what do you really expect the conditional operator to do. It seems that what the operator does and what you expect are in disagreement Dec 4, 2013 at 17:50
  • 6
    @Tommy -- FWIW, pass doesn't work in a python conditional expression either ... 1 if True else pass is invalid.
    – mgilson
    Dec 4, 2013 at 17:54
  • 1
    Okay, well if you want to ask the question without any modifiers, then no, C++ does not have anything like pass in all semantic constructions. Dec 4, 2013 at 17:55
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.

1
  • 1
    you can't write this inside a function Mar 28, 2019 at 15:26
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 */
}
7
  • It may be valid, but it's still a bad idea. Dec 4, 2013 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. Dec 4, 2013 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. Dec 4, 2013 at 18:20
  • 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. Dec 4, 2013 at 20:14
  • 1
    pythons not broken it wont let you put a pass in a ternary statement either Dec 4, 2013 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};
}
0
3

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

0
3

As @bboonn suggests:

 if (some_flag)
   ; // Do nothing

 else if (some_other_flag)
   do_something();
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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.