What is the advantage of adaptors over lambda expression in sigc++?

67 views Asked by At

Background

Suppose I have the following template function to parse an input parameter:

template <typename T>
bool my_parse(const Glib::ustring& data, T& storage) {
  // Logic: data >> storag
}

But for working with Glib::OptionGroup, I need the following signature for a callback function (slot),

bool parse(const Glib::ustring& X, const Glib::ustring& data, bool Y);

where X and Y are irrelevant and the second parameter corresponds to the first parameter in my_parse. Now in order to drop the two unwanted parameters and inject the storage-parameter, I can use adaptors as follows:

unsigned int my_storage;
auto my_func = sigc::ptr_fun<bool, const Glib::ustring&, size_t&>(&my_parse);
auto my_slot = sigc::hide<0>(sigc::hide(sigc::bind(my_func, my_storage)));
og.add_entry(my_option_entry, my_slot);

The same can be achieved using a lambda expression, in which case the code reads:

auto my_lambda = [&](const Glib::ustring& name, const Glib::ustring& value, bool) {
  std::ignore = name;
  return my_parse(value, my_storage);
};
og.add_entry(my_option_entry, my_lambda);

Question

IMO, the second piece of code is much more readable and can be readily understood without looking up bind and hide in the documentation. Is there any advantage in using the adaptor solution?

0

There are 0 answers