Consider the following code related to const links:
const int & func (const int &x)
{
return x;
}
struct Foo {
Foo (const int &x)
: m_x(x) {}
const int & getX ()
{ return m_x; }
const int &m_x;
};
I would like to know that, if any, is now allowed:
int x = func(int(7));
int y = Foo(int(7)).getX();
Is there any guarantee that the temporary object intstill exists before it is used by the destination or getX?
UPDATE : So it seems that it is safe, but why exactly?
- Is this because temporary links are recursively linked to constant links and are guaranteed to exist as long as there are linked links to them?
- Or is it because they are guaranteed to exist for the duration of full expression?
Consider the case of an edge that stores a pointer instead of a link:
struct Foo {
Foo (const int &x)
: m_x(&x) {}
const int & getX ()
{ return *m_x; }
const int *m_x;
};
int y = Foo(int(7)).getX();
, 1) , . 2) , .