tags:

views:

106

answers:

2

hello.

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

+5  A: 

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.

Tyler McHenry
+10  A: 

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.

Mike Seymour
It's 12.2 in C++98 as well.
Tyler McHenry