Re: Replace Gtk::manage() with std::unique_ptr<>?

On Sun, 2016-02-07 at 22:51 +0100, Dr. Diether Knof wrote:

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");
  notebook.append_page(std::move(widget), "something");
OK, but widget does no longer point to the botton, compare with the
example at
  notebook.child_property_tab_expand(*widget) = true;
Should lead to a segmentation fault.

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.)

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

  auto widget = std::make_unique<Button>("some button");
  Gtk::Widget* widget_to_expand = *widget;
Wrong, right is:
Gtk::Widget* widget_to_expand = widget.get();
  notebook.append_page(std::move(widget), "something";

Another idea is to change the add-methods to return a pointer to the
added. So the code can be written as (for clearity with Gtk::Button*
of auto):
Gtk::Button* button =

For this, Container::add_unique() must be a (simple?) template, so it
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();
  return p;
virtual void Container::add(std::unique_ptr<Gtk::Widget> widget);

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
murrayc murrayc com

[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]