4 ms·
> Who wants to type std::unique_ptr<Foo> when instead you can write Foo *? struct Foo{ std::unique_ptr<Foo> Ptr; } Foo::Ptr foo;
by happyweasel 5y ago
> Who wants to type std::unique_ptr<Foo> when instead you can write Foo *?
struct Foo{
std::unique_ptr<Foo> Ptr;
}
Foo::Ptr foo;
- HelloNurse 5y agoAngle brackets don't look good, but explicitly stating that the pointer is a unique_ptr rather than forcing the reader to figure out where Foo::Ptr is defined (presumably a different file) in order to look up the actual type has a lot of value.
- codeflo 5y agoCodebases that do that are very rarely const correct, however. (Personally, I don’t think typing unique_ptr<Foo> or unique_ptr<const Foo> is such a big deal. But then, I also consider having “using namespace std” the only somewhat sane way to use the STL.)
- omegalulw 5y ago+1. In modern C++ you use can use auto most of the time anyway :)
- tialaramex 5y agoThe thing about auto compared to Rust's inferred types is that when Rust isn't sure it won't infer anything and punts the problem back to you. let sandwiches = inventory.filter(delicious_cheeses).map(make_sandwich).collect(); ... won't compile because Rust can't infer what sort of container sandwiches is. Is it a vector? A list? Something custom? When C++ can't be sure in some places auto is obligatory anyway, so, too bad, you get what you're given. There are rules for what you get, but you might have reasonably intended something else, and so the effect is surprising.
- MauranKilom 5y ago> But then, I also consider having “using namespace std” the only somewhat sane way to use the STL. I hope you never ever do that in a header :)
- dataflow 5y agoI think you forgot to say typedef.
- happyweasel 5y agoyes
- mrcode007 5y agousing FooPtr = std::unique_ptr<Foo>; Does a job in an idiomatic way
- MauranKilom 5y agoCTAD also allows you to write std::unique_ptr foo = std::make_unique<Foo>();