considering following code , Why I can’t use the assignment notation here , Why that is considered to be an implicit conversion.
shared_ptr<string> pNico = new string("nico"); // ERROR implicit conversion
shared_ptr<string> pNico{new string("nico")}; // OK
The constructor is explicit to prevent somebody from doing something like this:
void foo(std::shared_ptr<std::string> s) { }
int main()
{
std::string s;
foo(&s);
}
If it was implicit, the shared_ptr could take ownership of a stack allocated variable and try to delete it..which wouldn't make sense.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With