std::unique_ptr<char[]> func(void)
{
auto str = std::unique_ptr<char[]>(newchar[3]); // <-- wrap array pointer in std::unique_ptr
str[0] = 'a';
str[1] = 'b';
str[2] = '\0';
return str; // <-- this implicitly "moves" the std::unique_ptr to the caller, thus transferring the ownership of the array from this function to the caller
}
int main()
{
cout << func().get() << endl; // <-- when the temporary std::unique_ptr object is destroyed, then the wrapped array is delete[]'ed automatically!
return 0;
}
Just sayin', if you're going to using a unique_ptr<array>, you should really just consider using a string or vector. Of course, demonstrating its use for educational purposes like you did is fine, I just personally wouldn't use that in actual code.
Just sayin', if you're going to using a unique_ptr<array>, you should really just consider using a string or vector. Of course, demonstrating its use for educational purposes like you did is fine, I just personally wouldn't use that in actual code.
Yeah - but still better than returning a raw pointer and needing delete []...
Small point - but stream extraction doesn't require .get() with a smart pointer to access the contents. In the above, f() works fine so no need for f().get()
However even if the return type of func() is known (const char*) this doesn't give us any info as to where pointer ownership resides. Do we need to delete[] it? In this case no as it wasn't obtained from a new and ownership isn't passed. But if the return address was obtained from a new then yes, we would need to delete[] it as ownership has been 'passed' but this can't be known from just the function return type.
If the ownership of the memory is to be passed, then a return type of std::unique_ptr makes this clear and the caller knows (and also doen't need to do anything about it as delete [] will be automatic when needed.
However even if the return type of func() is known (const char*) this doesn't give us any info as to where pointer ownership resides. Do we need to delete[] it?
Rust is a programming language that is supposed to be a "better C++" and replace C/C++ as The Sole Programming language eveh. At least according to Rust advocates and zealots.
I am not of them.
Rust is just the latest "shiny thing." Similar to Java, Rust is a nice idea next-step language that has IMO a niche applicability.
C/C++ are still dominant life forms for creating a wide variety of compiled applications. I don't see C/C++ being replaced any time soon.
Both C++ and Rust support static checking of ownership.
C++'s approach to ownership is based on conventions, and is opt-in. To opt-in you make disciplined use of unique_ptr, value semantics, and RAII in your program.
Rust's approach to ownership is based on the definition of the type system, and is opt-out. To opt-out you put your code in an "unsafe block" which relaxes the type checker.
Rust does have some good ideas here. There's value in getting safety guarantees for free -- even though it's not going to be "free" all the time.
I expect it's already dogma to avoid their unsafe keyword.