简体   繁体   中英

C++ constant temporary lifetime

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

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 .

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM