Nicer construction via helper function: Gtk::make_managed<WidgetType>(ctor, args)
@dboles
Submitted by Daniel Boles Link to original bug (#795816)
Description
Creating a widget whose lifetime management is left to its parent container is currently done like so, passing through Gtk::manage()
to re-float the reference:
auto button = Gtk::manage( new Gtk::Button{"label", use_underline} );
I feel like this is very clunky syntactically, and - call me a modern C++ purist, I guess - I just don't like seeing or having to write "new"! I was going through my largest project trying to avoid holding widgets as members when I never need them beyond narrow scope, and I'm quite put off by how the resulting code looks (even if my header files will be far lighter after that's complete).
I think it would be nicer if we provided a templated helper function to make the syntax simpler and abstract away the allocation. I imagine this working much like std::make_unique<>()
, std::make_shared<>()
, et al.:
auto button = Gtk::make_managed<Gtk::Button>("label", use_underline)
;
where this could be implemented in terms of Gtk::manage()
for the templated type but handle the allocation and perfectly forward the ctor arguments for the user.
I realise that ends up being the same number of characters... but I still think it looks better, and it feels far more logical to write than what the current manage() syntax enforces, and you might think up a shorter function name anyway.
Any thoughts? I know this is a minor cosmetic thing, but maybe it's easy enough to implement that it can be worthwhile to get nicer/more modern looking syntax.