I want to create a wchar_t* which length is dynamic. So I decided to write the following function:
std::unique_ptr<wchar_t> mem::TO_WCHAR_T_PTR(char* str)
{
size_t len = strlen(str) + 1; // Size of my wchar_t string
std::unique_ptr<wchar_t> wStr_ptr(new wchar_t[len]); // Compiler supports C++17
size_t convertedChars = 0;
mbstowcs_s(&convertedChars, wStr_ptr.get(), len, str, _TRUNCATE);
return wStr_ptr; // Is this save to use?
}
int main(int argc, char** argv)
{
char str[] = "Hello world";
auto ptr1 = TO_WCHAR_T_PTR(str);
// Do some stuff ...
return 0;
} // ptr1 out of scope so it gets deleted or was it already deleted(?)
My question: Is it ok to use smart-pointert by value or do I need to pass them by reference? I know about move when using unique_ptr I just want to make sure I can use the smart_ptr like this. The next question: Does it work with shared_ptr as well?