The code does not really show that it is legal in C++. It simply shows that your compiler supports a non-standard compiler extension. You have to consult your compiler docs to find out whether the lifetime of the temporary is extended. Your experiment appears to show that it is extended.
Nevertheless, your code is ill-formed in standard C++. If you disable compiler extensions in that compiler by using /Za
option, it will also refuse to accept your code:
error C2440: 'initializing' : cannot convert from 'A' to 'A &'
Alternatively, to avoid using /Za
(which is apparently broken) you can do
#pragma warning(error : 4239)
or change the corresponding project setting under C/C++ -> Advanced -> Treat Specific Warnings As Errors
to prohibit this specific functionality in a more targeted manner.