Why use ++i instead of i++ in cases where the value is not used anywhere else in the statement?

前端 未结 7 1702
情深已故
情深已故 2020-12-02 20:52

I\'m well aware that in C++

int someValue = i++;
array[i++] = otherValue;

has different effect compared to

int someValue =          


        
相关标签:
7条回答
  • 2020-12-02 21:20

    If we ignore force of habit, '++i' is a simpler operation conceptually: It simply adds one to the value of i, and then uses it.

    i++ on the other hand, is "take the original value of i, store it as a temporary, add one to i, and then return the temporary". It requires us to keep the old value around even after i has been updated.

    And as Konrad Rudolph showed, there can be performance costs to using i++ with user-defined types.

    So the question is, why not always just default to ++i?

    If you have no reason to use `i++´, why do it? Why would you default to the operation which is more complicated to reason about, and may be slower to execute?

    0 讨论(0)
  • 2020-12-02 21:25

    Yes, for performance reasons. In case of post increment a copy of the variable i needs to be made before incrementing so that the old value can be returned (eventhough you are not using the return value). In case of pre-increment this copy is not required.

    0 讨论(0)
  • 2020-12-02 21:30

    The C++ FAQ Lite has a nice discussion of i++ vs. ++i here:

    [13.15] Which is more efficient: i++ or ++i?

    In particular, with respect to which form to use within a for loop, the FAQ expresses a preference for ++i. Here's the text:

    So if you're writing i++ as a statement rather than as part of a larger expression, why not just write ++i instead? You never lose anything, and you sometimes gain something. Old line C programmers are used to writing i++ instead of ++i. E.g., they'll say, for (i = 0; i <10; i++) .... Since this uses i++ as a statement, not as a part of a larger expression, then you might want to use ++i instead. For symmetry, I personally advocate that style even when it doesn't improve speed, e.g., for intrinsic types and for class types with postfix operators that return void.

    0 讨论(0)
  • 2020-12-02 21:32

    Look at possible implementations of the two operators in own code:

    // Pre-increment
    T*& operator ++() {
        // Perform increment operation.
        return *this;
    }
    
    // Post-increment
    T operator ++(int) {
        T copy = *this;
        ++*this;
        return copy;
    }
    

    The postfix operator invokes the prefix operator to perform its own operation: by design and in principle the prefix version will always be faster than the postfix version, although the compiler can optimize this in many cases (and especially for builtin types).

    The preference for the prefix operator is therefore natural; it’s the other that needs explanation: why are so many people intrigued by the use of the prefix operator in situations where it doesn’t matter – yet nobody is ever astonished by the use of the postfix operator.

    0 讨论(0)
  • 2020-12-02 21:32

    As you noted - it does not matter to the result.

    There is a performance consideration for non-primitive types.

    Also semantically using pre-increment is usually clearer in showing the intention of a test when the return value is used, so its better to use it habitually than post-increment to avoid accidentally testing the old value.

    0 讨论(0)
  • There is little reason to favour pre-increment over post-increment when you are talking about natural types like integers. The compiler is typically able to generate the same code in both cases anyway, assuming you don't use the return value. This is not true for more complex types, such as iterators.

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