Q:

boost::function type compatibility, how's it done? 

explain this one:
struct X
{
  void foo(int arg) { cout << "X" << arg << endl; }
};

struct Y
{
  void bar(int arg) { cout << "Y" << arg << endl; }
};

int main(int argc, char *argv[])
{
  X x;
  Y y;

  mem_fun1_t<void, X, int> f1 = std::mem_fun(&X::foo);  
  boost::function<void (int)> f11 = std::bind1st(f1, &x);
  f11(2);

  mem_fun1_t<void, Y, int> f2 = std::mem_fun(&Y::bar);
  boost::function<void (int)> f22 = std::bind1st(f2, &y);
  f22(2);

  f11 = f22;  // WOW, THIS IS ALLOWABLE
}

How does Boost work, under the covers, to allow the line f11 = f22? 
Seems unusual because f11 is a functor whose operator(int) calls an X::foo(int) with a this of x, so it would seem to be type specific to X, and then when I do same with f2/f22 its specific to Y, so how can the line f11 = f22 be allowed? 
I actually want to do the f11 = f22 line, but I was surprised to see this was allowed, and am trying to understand how this isnt a type mismatch. 
I know, "use the source, Luke", but the Boost sources are difficult to follow/comprehend with everything going on in there. 
If you could, for your answer, show the classes that are expanded by the templatization, that would help. 
I was thinking it was getting away with this by void* casting or something like that, but that seems like a hack, and beneath Boost to stoop down to that level.  So whassup? 
BTW, if you haven't encountered this kind of thing before, you should at least be surprised at this bit of magic, since you can't say "x = y" above, as that would clearly be disallowed since they are different types and there is no X::operator=(Y&), so that's where this amazement comes from - its a clever piece of subterfuge.  

A:

The technique used is known as type erasure. 
I'll demonstrate with a toy class, written in C++11.   Many of the details will not be accurate, but the general technique is:
struct nullary_impl {
  virtual void invoke() const = 0;
  virtual ~nullary_impl() {}
};
typedef std::shared_ptr<nullary_impl> nullary_pimpl;
struct nullary_func {
  nullary_pimpl pimpl;
  nullary_func() = default;
  nullary_func( nullary_func const& ) = default;
  template<typename F>
  nullary_func( F const& f );
  void operator()() const {
    pimpl->invoke();
  };
};
template<typename T>
struct nullary_impl_impl:nullary_impl {
  T t;
  virtual void invoke() const override {
    t();
  }
  nullary_impl_impl( T const& t_ ):t(t_) {}
};
template<typename F>
nullary_func::nullary_func( F const& f ):
  pimpl( std::make_shared( nullary_impl_impl<F>(f) )
{}

now in this case, the class is not a template class, but it being a template class isn't the important part. 
The important part is that it has a template constructor.   This template constructor creates a custom typed object, based on what type we construct the nullary_func with, and then stores a pointer to the custom typed object's abstract base.   This abstract base has a virtual method of some kind that we call.   The custom child type implements that virtual method, and invokes () on the underlying type. 
Now, the shared_ptr is probably not what boost uses (probably some kind of value_ptr), and in function you have both a template class and template constructor, and there are lots of other details.   And you can implement your own dynamic dispatch instead of virtual calls if you want to get fancy.   And in real C++11 I'd be doing perfect forwarding with all the issues of a perfect forwarding single argument constructor that entails.