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



2013-07-26 13:03, Murray Cumming skrev:
C+11 has std::function<> which is a bit like sigc::slot, though C++11
doesn't have anything like sigc::signal<>. I played with that here:
http://www.murrayc.com/permalink/2013/07/08/c-in-glom-shared_ptr-and-slotsfunctions/

I've noticed that std::function<> works with libsigc++, as in the code
below, but I wonder why it works. Can anyone explain?


#include <iostream>
#include <string>
#include <sigc++/sigc++.h>
#include <functional>

void on_print(const std::string& str)
{
  std::cout << str;
}

int main()
{
  sigc::signal<void, const std::string&> signal_print;
  
  std::function<void(const std::string&)> slot = &on_print;
  signal_print.connect(slot);
  
  signal_print.emit("hello world\n");

  return 0;
}

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.

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.

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.

Kjell




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