Why does temporary struct member not have the expected value in C++?

Viewed 77

Consider this code:

#include<iostream>

struct A
{
    int b;
};

int main()
{
    int c = (A() = A{2}).b;  // Why is c zero after this?
    std::cout << "c = " << c << std::endl;
    std::cout << "A.b = " << (A() = A{2}).b << std::endl;
}

In my mind this is two equivalent ways to print the same value, but I get this result (on GCC 7.3.0 under MinGW):

c = 0
A.b = 2

I would have expected c to be 2. Can anyone explain why it is 0?

0 Answers
Related