Is this behavior undefined or error with struct init?

Please consider this bit of code:

#include <iostream>

int main()
{
    struct A 
    { 
        int x; 
        int y; 
        int z; 

        int foo()
        {
            std::cout << "enter foo: " << this->x << "," << this->y << "," << this->z << std::endl;
            return 5;
        }       

        int moo() 
        { 
            std::cout << "enter moo: " << this->x << "," << this->y << "," << this->z << std::endl;
            this->x = 1;
            this->z = 10;
            return 2; 
        }
    };

    A b { b.foo(), b.z = b.moo(), 3};

    std::cout << "final: " << b.x << "," << b.y << "," << b.z << std::endl;

    return 0;
}

The result in my VS2017 (x64 release):

enter foo: 0,0,0
enter moo: 5,0,0
final: 1,2,3

Result from ideone.com (gcc 6.3) https://ideone.com/OGqvjW ):

enter foo: 0,0,3
enter moo: 5,0,3
final: 1,2,2

One compiler sets the member zto 3 immediately, before everything, then overwrites it when calling methods and assignments, and the other at the very end, after all.

Q. What will be the explanation for this behavior?

Thank.

+4
source share
1 answer

Yes, this behavior is undefined:

int foo()
{
    std::cout << "enter foo: " << this->x << "," << this->y << "," << this->z << std::endl;
    //                            ~~~~~~~           ~~~~~~~           ~~~~~~~
}       

At the time of the call foo() x, yand znot yet initialized. From [dcl.init] / 12 :

, . , , , , ([expr.ass]). [...] , undefined, : [...]

. , x, y z undefined. :

int x;
std::cout << x; // ub

, . , A b{ b.foo(), b.z = b.moo(), 3}; , , b UB. xskxzr , , , , , int . b . , .

+7

Source: https://habr.com/ru/post/1691544/


All Articles