GrkGtk::manage()
solves the specific problem of lifetime management for hierarchies of widgets. And it solves it well.
Smart pointers (std::shared_ptr
in particular) have broader application range, and therefore will be less efficient when used to address this specific problem. Lifetime management for hierarchies of widgets can be solved with shared_ptr
, but it will be:
- not as concise as using
manage()
(as you pointed out yourself); - less efficient in terms of memory usage, since using
shared_ptr
'ss will introduce memory overhead as:shared_ptr
has a memory footprint is going to contributeof its reference to your widget'sits reference-count, which will use size (as- as opposed to manage()manage()
, which as far as I'm aware uses variables, such as, sayGtk::Object.referenced_
, that are part of yourWidget
base class anyways, no matter you usemanage()
on it or not);already at all times. So in case, if you have a significant amountnumber ofWidgets
it, that size difference might become an issue worth considering; - not as mainstream as using
manage()
in Gtkgtkmm (haswhich has quite a few consequences, including clarity and maintainability).
As of idea of favoring std::shared_ptr
over Gtk::manage()
because itshared_ptr
is part of C++ Standard, while Gtk::manage()
is not - I'm not sure it is going to be a game changer for average application, as by not using manage()
you don't cut your dependency on Gtk anyways. So your application is not going to gain any better portability if you'd go for shared_ptr
.
I would rather leverage native Gtkgtkmm API, for sake of clarity and efficiency.
pP.sS. there: There is actually a smart pointer GLibGlib::RefPtr
, which can addresshandles lifetime management for Widgets
in a manual waycertain gtkmm objects. Again, as it is a feature native to GLibGlib, it leverages built-in facilities of Glib::ObjectBase
, and is therefore more efficient then std::shared_ptr
for certain applications (e.g. reference to reference counter is not stored in the pointer object, as reference counter is embedded tofor the object, and therefore RefPtr
is more lightweight than shared_ptr
, which will make a significant difference if you have many pointers to a few objects)reasons explained in the 2nd point above.