Is this using a temporary std :: string right?

std::string getMyString() { return <make a string>; }

...

HANDLE something = OpenSomething(getMyString().c_str(), ...);

I read the Guaranteed lifetime of a temporary in C ++ , and I believe that the temporary string will live until the assignment is evaluated, i.e. enough to do this work as expected.

Once before starting a std::stringlifecycle related error (I don't remember what it was), I would prefer to double check ...

+5
source share
3 answers

Yes, that’s fine. :-)

The line will be destroyed at the end of the instruction in half-pitch.

+7
source

, , , .

, char*, - , OpenSomething , .

+8

Unless you use any other argument OpenSomthingto return a pointer to getMyString.c_str(), everything will be fine.

0
source

All Articles