Boost logo

Boost :

From: Matthias Hoffrichter (TrollCoder_at_[hidden])
Date: 2003-02-18 22:56:55


Hi,

I want to encapsulate boost::find in a template method in a base class for easier use.
Here is some code:

#include <boost/function.hpp>
#include <boost/bind.hpp>

class CWindow {
public:
 CWindow() {
  SetEventHandler(&CWindow::OnCreate); // this call works
 }
 long OnCreate() {
  return 0;
 }
 template<typename T> void SetEventHandler(long (T::*Function)()) {
  boost::function<long> EventFunction = boost::bind(Function, this);
  // ...
  // Add EventFunction into a std::map
 }
};

class CButton : public CWindow {
public:
 CButton() {
  SetEventHandler(&CButton::OnPaint); // this call doesn't compile
 }
 long OnPaint() {
  return 0;
 }
};

int main() {
 return 0;
}

The SetEventHandler call in the CButton's constructor generates 2 errors on VC++ 7.0:

mem_fn_template.hpp(37): error C2440: 'newline' : 'CWindow *' can't be converted in 'CButton *'
mem_fn_template.hpp(37): error C2647: '->*' : 'const boost::_mfi::mf0<R,T>::F' can't be dereferenced in '$T'

It also doesn't compile on g++ 3.2.

If I copy & paste the template method in every derived class the code compiles well, but this can't be a good soluation. :(
And if i outcomment "boost::function<long> EventFunction" it also works, but of course I need to work with boost::bind's return value. ;-)

Is there a way to solve this problem?

cu,
Matthias



Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk