If I am having the following:
#include <iostream>
#include <string>
#include <type_traits>
struct A{
~A(){std::cout << "killed '" << a <<"'\n";}
int a = 3;
};
template<typename T>
void foo(T&& r){ // Is r still valid here???
std::cout << "Running: " << r.a << "\n";
}
int main()
{
A* a = new A{3};
foo(true ? A{4} : *a);
}
I get no suprising result:
Running: 4
killed '4'
http://coliru.stacked-crooked.com/a/b218bef991ef24d2
However I get a crash with MSVC 16 (VS 19) and C++14...
I am wondering if in the example above there is no lifetime extension. There should be, since I am binding a prvalue A
to a A&&
(the T&&
) which is a rvalue-reference. And lifetime extension is guaranteed when binding to rvalue-reference or const lvalue-references.