Search code examples
c++templatesbindingg++gtkmm

compilation error with binding a template function to a gtkmm signal


I have some callback functions :

class someclass
{
  private:
  bool someCB1(GdkEventFocus*,GtkEntry*);
  template<class T> bool someCB2(GdkEventFocus*,T*);
};

somewhere in the code of someclass I have a Gtk::Entry* entry. If I connect someCB1 :

entry->signal_focus_out_event().connect( sigc::bind<Gtk::Entry*>( sigc::mem_fun( this, &someclass::someCB1 ), entry ) );

this one works, but in my case I want to use someCB with different kinds of Gtk::Widget, so I wrote the template function someCB2

to connect someCB2 I wrote :

entry->signal_focus_out_event().connect( sigc::bind<Gtk::Entry*>( sigc::mem_fun( this, &someclass::someCB2 ), entry ) );

this line failed at the compilation, errors are very numerous (I cannot scroll my console to the first one, but the last ones are similar, so I guess like the rest). Here the last one :

/usr/include/sigc++-2.0/sigc++/functors/mem_fun.h:6356:1: note:   template argument deduction/substitution failed:
/home/user/chicken.cc:158:111: note:   couldn't deduce template parameter ‘T_arg1’
   entry->signal_focus_out_event().connect( sigc::bind<Gtk::Entry*>( sigc::mem_fun( this, &someclass::someCB2 ), entry ) );

can someone tells me what I mess ?


Solution

  • When you use &someclass::someCB2 the compiler has no chance to deduce what type T should be when using it with mem_fun(). If the address of the class is directly used with something allowing to deduce the template argument it would work.

    In your case you probably want to use something like the below instead:

     static_cast<bool (someclass::*)(GdkEventFocus*, GtkEntry*)>(&someclass::someCB2)
    

    Alternatively you can also specify the template argument directly:

    &someclass::someCB2<GtkEntry>