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

前端 未结 7 547
轻奢々
轻奢々 2021-01-01 10:26

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 do

相关标签:
7条回答
  • 2021-01-01 10:28

    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.

    0 讨论(0)
  • 2021-01-01 10:28

    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 */
    }
    
    0 讨论(0)
  • 2021-01-01 10:32

    As @bboonn suggests:

     if (some_flag)
       ; // Do nothing
    
     else if (some_other_flag)
       do_something();
    
    0 讨论(0)
  • 2021-01-01 10:32

    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 讨论(0)
  • 2021-01-01 10:33

    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;
    
    0 讨论(0)
  • 2021-01-01 10:43
    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

    0 讨论(0)
提交回复
热议问题