Let's assume we have a char string from somewhere else, which is later on used as an input parameter on a function which accepts wide strings.
In Variant 1, a UnicodeString is created from the char string and in a second step used as a wchar_t* pointer to the LoadLibrary() function.
Is it always safe to shorten this into one code line as written in Variant 2? In particular, will the temporary UnicodeString object always live long enough during its usage in the function itself? Could there be any special circumstances which would destroy the object to early?
char *libstr = "test.dll";
//...
//Variant 1
UnicodeString us(libstr);
HINSTANCE lib = LoadLibrary( us.w_str() );
//Variant 2
HINSTANCE lib = LoadLibrary( UnicodeString(libstr).w_str() );
Thanks,
Edit: @David Heffernan Thanks for pointing to the answered question regarding Life span of temporary arguments in C++ As this is a specific question for UnicodeString and AnsiString objects in Delphi and C++ Builder, and there these objects are coded in the VCL using Pascal, I am still wondering about the topic and I feel your link does not cover the topic 100%, although it is very helpful on the subject. Thank you.