Reputation: 51465
Can you please tell me if such code is correct (according to standard):
struct array {
int data[4];
operator const int*() const { return data; }
};
void function(const int*) { ... }
function(array()); // is array data valid inside function?
Thank you
Upvotes: 7
Views: 261
Reputation: 254431
Yes. The temporary object is valid until the end of the full expression in which it is created; that is, until after the function call returns.
I don't have my copy of the standard to hand, so I can't give the exact reference; but it's in 12.2 of the C++0x final draft.
Upvotes: 12
Reputation: 76660
Yes. Temporaries are valid until the end of the full expression in which they are created. Therefore the nameless array temporary would be valid until the call to function
returns, and so its data
member would be as well.
Upvotes: 6