I wrote the following code to test this:
struct X
{
char* x;
X()
{
x = new char('a');
}
~X()
{
*x = 'b';
delete x;
}
};
void foo(const X& x)
{
}
void goo(X& x)
{
}
int main()
{
foo(X());
goo(X());
}
The destructors for the temporaries are called after both functions exit, but I thought you can only bind a temporary to a const
reference. Why does goo
work then?
Is it UB and MSVS is wrong, or is it ok?