Listing 3

// Listing 3: ChildWindows_basic_range Basic Indirect Range class
class ChildWindows_basic_range
  : public stlsoft::basic_indirect_range_tag
{
public:
  typedef HWND                      value_type;
  typedef ChildWindows_basic_range  class_type;

public:
  ChildWindows_basic_range(HWND hwnd)
    : m_hwnd(hwnd)
  {}
  template <typename F>
  F for_each_cancelable(F f)
  {
    return for_each_cancelable_(f, m_hwnd);
  }
  template <typename F>
  F for_each_cancelable(F f) const
  {
    return for_each_cancelable_(f, m_hwnd);
  }
private:
  template <typename F>
  static F for_each_cancelable_(F f, HWND hwnd)
  {
    // Local structure (so can get local callback function)
    struct Internal_
    {
     typedef F fn_t; // Need it as member type, for VC 7.0
     static BOOL CALLBACK for_each_cancelable_Proc(HWND hwnd, LPARAM lParam)
     {
        // Get back the address of the function from lParam
        void  *pv = reinterpret_cast<void*>(lParam);
        fn_t  *pf = static_cast<fn_t*>(pv);
        return (*pf)(hwnd);
     }
    };
    // Do the enumeration, passing in the "local" callback function
    // , and the address of the function object passed in from client code.
    ::EnumChildWindows( hwnd, Internal_::for_each_cancelable_Proc,
                                        reinterpret_cast<LPARAM>(&f));
    return f;
  }
private:
  HWND  m_hwnd;
};