3 ms·
Is `shared_ptr` just used for convenience? I think `unique_ptr` would be excellent for this tree structure. It's not too bad to traverse the tree using non-own
by stabbles 7y ago
Is `shared_ptr` just used for convenience? I think `unique_ptr` would be excellent for this tree structure.
It's not too bad to traverse the tree using non-owning raw pointers.
- nemetroid 7y agoI don't think there's need for smart pointers at all, really. The "children" member could just be an unordered_map<T, Node>. Edit: on second thought, having a class contain a container of itself actually isn't allowed (though you would think it would be in cases like this). Edit again: apparently it depends on the container. unordered_map does not allow instantiation with an incomplete type, but other containers do (e.g. vector or map).
- stabbles 7y agoYes, I was playing around with it as well. It seems the support has been out there for a while, but officially since C++17 http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n4371.html http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/n437... was accepted, s.t. std::vector, std::list and std::forward_list can work with incomplete types: struct example { std::vector<example> xs; };
- saagarjha 7y agoI believe containers can contain incomplete types as of C++17, but you cannot use their members.
- xorz57 7y agoYes `unique_ptr` would be better!