Temporaries and arrgegate member initializers c++17

What are the rules around this? This example seems to work with GCC and clang; is this just UB or is there something in the standard that explains it?

https://wandbox.org/permlink/MZhWkSrUOLW6QPzP

The unique_ptr one surely has to be UB?
Last edited on
f() as is called from line 29 should cause undefined behavior, because line 16 will try to write to an object that's no longer there (the temporary int created with a value of 2 on line 26).

Line 33 also has undefined behavior, although not because of the move. The move should have no side-effect on the parameter on instances of T. It should be a no-op.
It has undefined behavior because t.s.i is still pointing to something that's not there.

Line 40 has undefined behavior for the same reason.

AFAICT, leaving or removing the calls to alloca() should not change the undefinedness of the behavior.

PS: By the way, on all implementations I've seen, inner blocks inside a function do not move the stack pointer. So for example
1
2
3
4
5
6
7
8
9
10
11
12
{
    char x2;
    char *p1 = &x2;
    alloca(1024);
    char x3;
    char *p2 = &x3;
}
char x4;
char *p3 = &x4;
alloca(1024);
char x5;
char *p4 = &x5;
p3 and p1 will very probably point to different places, as will p4 and p2.
Last edited on
Topic archived. No new replies allowed.