On Sun, 2016-02-07 at 22:51 +0100, Dr. Diether Knof wrote: > Hello, > > I think, to use std::unique_ptr and std::move instead of Gtk::manage > is a good > idea, but it is more difficult to use: > > > auto widget = std::make_unique<Button>("some button"); > OK > > notebook.append_page(std::move(widget), "something"); > OK, but widget does no longer point to the botton, compare with the > example at > http://en.cppreference.com/w/cpp/utility/move > > notebook.child_property_tab_expand(*widget) = true; > Should lead to a segmentation fault. [snip]
Yes, it does, and that's the issue here. (I've heard various discussions of what the C++ standard specifies about how valid or defined the moved-from object is, but I would indeed prefer to avoid any use after move.) [snip] > So, in order to create a widget, move it into a container and use it > afterwards, we have to create two pointers (the unique-pointer and a > normal > pointer): > > > auto widget = std::make_unique<Button>("some button"); > OK > > Gtk::Widget* widget_to_expand = *widget; > Wrong, right is: > Gtk::Widget* widget_to_expand = widget.get(); > > notebook.append_page(std::move(widget), "something"; > OK > > > Another idea is to change the add-methods to return a pointer to the > object > added. So the code can be written as (for clearity with Gtk::Button* > instead > of auto): > Gtk::Button* button = > container.add_unique(std::make_unique<Gtk::Button>("a > button")); > > For this, Container::add_unique() must be a (simple?) template, so it > returns > the exact type for the pointer (Gtk::Button*) and not Gtk::Widget*: > template<class X> > X* Container::add_unique(std::unique_ptr(X) widget) > { > X* p = widget.get(); > this->add(std::move(widget)); > return p; > } > virtual void Container::add(std::unique_ptr<Gtk::Widget> widget); [snip] Yes, I've considered this, but I'm still hoping for something nicer. I think I'd be content with needing a call to a getter method after a std::move(). I think that would make sense to people. But, for simple setup code, to arrange widgets in their initial working state, it would be nice if our API didn't require this. At the moment it's only necessary in a few places, and maybe we can improve them. -- Murray Cumming murr...@murrayc.com www.murrayc.com _______________________________________________ gtkmm-list mailing list gtkmm-list@gnome.org https://mail.gnome.org/mailman/listinfo/gtkmm-list