Is double pre-incrementing undefined behavior? Like ++++x or even ++++++++x. I think it must be since it all occurs between sequence points. But what could possibly go wrong? Or do you just not ask that question and automatically stay away from any undefined operations? Is double incrementing ever routinely used? Thanks.

It is undefined behavior because the compiler is not required to perform the increments in any specific order.

Or do you just not ask that question and automatically stay away from any undefined operations?

No and yes, respectively. Undefined behavior is super bad, but questioning why something is undefined improves understanding of the concept and helps one to avoid it in cases that aren't textbook examples.

Also ...

for examples like this:

int a = 1;
cout << "now a = " << (++(++a)) << endl; // two increments

// vs ...

int b = 1;
cout << "now b = " << (b += 2) << endl; // one addtion

// probably b is faster code

I think it must be since it all occurs between sequence points.

Sequence point rules were there prior to C++11. Now, those rules have been superceded by sequenced-before rules.

The question: does i++ + ++i; engender undefined behaviour is of relevance if and only if i is of a scalar type.

If i is of a user-defined type, ++i and i++ are function calls; the evaluations of the sub-expressions are indeterminately sequenced. This means that they may be evaluated in any order; but the two evaluations will never interleave even on a multi-core processor. Indeterminately sequenced does not imply undefined behaviour.

If i is a scalar, the evaluations are unsequenced and unsequenced side effects on a scalar leads to undefined behaviour.

Is double pre-incrementing undefined behavior? Like ++++x or even ++++++++x.

Assuming that x is a scalar, these are of interest:

If x is not of type bool, the expression ++x is equivalent to x+=1 - IS

The behavior of an expression of the form E1 op = E2 is equivalent to E1 = E1 op E2 except that E1 is evaluated only once.

AFAIK:

#include <iostream>

int main()
{
    int i = 1 ;
    struct A 
    { 
        int i ; 
        int j ; 
        A( int a, int b ) : i(a), j(b) {} 
        A& operator++() { return *this ; };
        A operator++(int) { return *this ; };
        A operator+ (A) { return *this ; }
    };

    ///////////  well-defined behaviour //////////////////////

    i = i + 1 ; // well defined behaviour (same as below)
    i += 1 ; // well defined behaviour

    i = ++i ; // well defined behaviour (same as below)
    i = ( i += 1 ) ; // well defined behaviour (same as below)
    i = ( i = i + 1 ) ; // well defined behaviour

    ++++i ; // well defined behaviour (same as below)
    ( i += 1 ) += 1 ; // well defined behaviour  (same as below)
    ( i = i + 1 ) += 1 ; // well defined behaviour  (same as below)
    i = ( i = i + 1 ) + 1 ; // well defined behaviour

    ++++++i ; // well defined behaviour (same as below)
    ( ( i += 1 ) += 1 ) += 1 ; // well defined behaviour

    int a[] = { i, ++i, --i } ; // well defined behaviour

    A a1 { ++i, ++i } ; // well defined behaviour (compare and contrast with line 51)

    A a2 = a1++ + ++a1 ; // no undefined behaviour (indeterminately sequenced != undefined behaviour)



    /////////// undefined behaviour //////////////////////////

    i = i++ ; // **** undefined behaviour (unsequenced modifications) ****

    i += ++i ; // **** undefined behaviour (unsequenced modification and access) ****

    std::cout << i << ++i ; // **** undefined behaviour (unsequenced modification and access) ****

    std::cout <<  ++i << ++i ; // **** undefined behaviour (unsequenced modifications) ****

    A a3( ++i, ++i ) ; // **** undefined behaviour (unsequenced modifications) ****

    void foo( int, int ) ; foo( i, ++i ) ; // **** undefined behaviour (unsequenced modification and access) ****
}

http://coliru.stacked-crooked.com/a/a43efd5038680084

++++++i; //parsed as (++(++(++i)))

i = (i,++i,++i); // Undefined Behaviour because there's no sequence point between ++i(right most) and assignment to i (i gets modified more than once b/w two SP)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.