Re: [sigc] How does std::function<> work with libsigc++



2013-07-29 10:26, Murray Cumming skrev:
On Fri, 2013-07-26 at 16:25 +0200, Kjell Ahlstedt wrote:


std::function<> is a functor (function object). Not much more than
that is required in order to convert it to a sigc::slot. What is
required, is that the compiler must be able to check that the return
type is correct. That's described at
https://developer.gnome.org/libsigc
++/unstable/group__sigcfunctors.html

Your example works because the return type is void. That's some kind
of default assumption in libsigc++. std::function<>s with other return
types work only if you include namespace sigc
{ SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE } in your code.
I guess that we should maybe emphasize the C++11 function<> case in that
list of alternative ways to make things work.
Agree. std::function<> should be mentioned where C++11 lambda expressions are mentioned. I might add some comments if no one else does.

sigc::trackable derived objects don't get any special treatment, if
they are included in std::function or a C++11 lambda expression.
Therefore I added sigc::track_obj(), but it has not yet made it into a
libsigc++ release.
So sigc::track_obj() creates a sigc::slot<> from a C++11 function<> or a
C++11 lambda?
Almost. It creates an adaptor, derived from sigc::adapts<>, like e.g. sigc::bind() and sigc::hide(). The adaptor can be inserted into a slot.

Wouldn't it be cleaner to just (or also) add a specific
connect(std::function<>) method overload that did this automatically?
Sure. I wish I knew how to do it. sigc::track_obj() forces you to manually specify which trackable objects the functor contains. This is not nearly as nice as the automatic detection of trackable objects in the rest of libsigc++. The enormous use of C++ templates in libsigc++ is near, or even beyond, the limits of my comprehension.

If there are no trackable objects in the std::function<>, or if you don't need automatic disconnection of the slot when a trackable object is deleted, then you can connect any functor, such as a std::function<>, directly to a signal, just as you would do with a slot. The description of sig::signal<>::connect() says

   * Any functor or slot may be passed into connect().
   * It will be converted into a slot implicitely.

There has been some discussion about libsigc++ and C++11 lambda
expressions over the last two years or so. I think most of what has
been said about C++11 lambda expressions can be directly translated to
std::function.
I wonder if we could ever deprecate sigc::slot<>, at least as public
API. I don't much like the new syntax compared to sigc::mem_fun(), but
we should expect people to want to use sigc::function<> now that it is
officially part of C++. And we could always leave sigc::mem_fun() as a
convenient way to create a std::function<>.

Is automatic detection of trackable objects important? If it is, I suppose someone must first find a way to automatically detect trackable objects in a std::function<>, and perhaps in a C++11 lambda expression. I don't know if it's possible.



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