3 ms·
Can we agree that the new "best default" way of instantiating is... pretty damn weird? auto foo = std::make_unique<Foo>(args); Think about explaining this
by de_keyboard 5y ago
Can we agree that the new "best default" way of instantiating is... pretty damn weird?
auto foo = std::make_unique<Foo>(args);
Think about explaining this to a new C++ developer.
This will feel much more natural to most developers:
var foo = new Foo(args);
- criddell 5y ago> Think about explaining this to a new C++ developer. How's this: The code allocates and initializes a new Foo instance with args. By using make_unique you guarantee that the destructor is called when foo goes out of scope for any reason.
- not2b 5y agoThe "best default" way of instantiating is Foo foo(args); Sometimes an object has to be on the heap as its own separate allocation (rather than on the stack or as a data member), but I notice that some new developers who come to C++ from some other language use the heap way too much.