Project

General

Profile

Should WObject support move operator?

Added by Plug Gulp over 4 years ago

Wt::WObject has explicitly deleted copy constructor and assignment. This creates a problem when a function tries to return a Wt::WTemplate, for e.g.

auto get_template(std::string const &name) -> Wt::WTemplate&&
{
    Wt::WTemplate t{Wt::WTemplate::tr(name)};

    t.addFunction("block", &Wt::WTemplate::Functions::block);
    t.addFunction("while", &Wt::WTemplate::Functions::while_f);
    t.addFunction("tr", &Wt::WTemplate::Functions::tr);

    return std::move(t);
}


Wt::WTemplate t{std::move(get_template("my_template"))};

This does not compile because Wt::WObject has explicitly deleted copy constructor and assignment operator.

If there is a move constructor and an assignment operator then this will compile? Should Wt::WObject support move operator?


Replies (1)

RE: Should WObject support move operator? - Added by Korneel Dumon over 4 years ago

Hi,

Wt is designed with dynamically allocated widgets in mind. For example, functions like WContainerWidget::addWidget() and WTemplate::bindWidget() take smart pointers as arguments, so you can't really pass widgets from the stack to them. When you dynamically allocate, you can just pass around pointers and this problem sort goes away by itself.

    (1-1/1)