Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

use lambda in connection with sigc library

I want to use lambda expressions in connection with goocanvas in gtk++. For my understanding this means that I must be able to put my lambda in a sigc++ functor.

I tried something like that:

sigc::slot<bool,  const Glib::RefPtr<Goocanvas::Item>& , GdkEventMotion* > slot2=
    [](  const Glib::RefPtr<Goocanvas::Item>& item, GdkEventMotion* ev)->bool
    {
        cout << "Lambda " << endl; return false;
    };

((Glib::RefPtr<Goocanvas::Item>&)item1)->signal_motion_notify_event().connect( slot2);

But this will not compile.

Is there a chance to get sigc working with lambdas or better gtkmm directly without the sigc++ intermediate :-)

like image 904
Klaus Avatar asked Dec 10 '12 17:12

Klaus


2 Answers

For void returning functions/methods with no arguments, it's pretty simple, e.g. (gcc 4.6, 4.7):

 fileButton.signal_pressed().connect([this]{on_file_button_clicked();});

Unfortunately I've not been able to get value returning or argument taking methods to compile, and have to resort to sigc::mem_fun() for those. There seems to be some recent activity to fix this, for example, this commit. If you have sigc++ version 2.2.11 or greater, you can try defining SIGC_FUNCTORS_DEDUCE_RESULT_TYPE_WITH_DECLTYPE, but I have no idea how well this works.

Also related is this bug report.

like image 85
ergosys Avatar answered Oct 05 '22 23:10

ergosys


I found the following code snipped which do the job. I have no idea how this interacts with the sigc++ lib, but I can use it for simple cases. Maybe someone else can take a look on it.

            #include <type_traits>
            #include <sigc++/sigc++.h>
            namespace sigc
            {   
                template <typename Functor>
                    struct functor_trait<Functor, false>
                    {   
                        typedef decltype (::sigc::mem_fun (std::declval<Functor&> (), 
                                    &Functor::operator())) _intermediate;

                        typedef typename _intermediate::result_type result_type;
                        typedef Functor functor_type;
                    };  
            }   

UPDATE: Libsigc is now able to handle lambas without any additional user code. The above code must be removed if any current versions is used.

like image 42
Klaus Avatar answered Oct 06 '22 00:10

Klaus