title: is there a better way to select correct method overload?
body: <p>Is this really the only way to get the correct address for an instance function:</p>
  <pre><code>typedef CBitmap * (CDC::* SelectObjectBitmap)(CBitmap*);
  SelectObjectBitmap pmf = (SelectObjectBitmap)&amp;CDC::SelectObject;
  </code></pre>
  <p>First, one has to create a typedef, and then one has to use that to force the compiler to select the correct overloaded method when taking its address?</p>
  <p>Is there no syntax that is more natural, and self-contained, such as:</p>
  <pre><code>SelecdtObjectBitmap pmf = &amp;CDC::SelectObject(CBitmap*);
  </code></pre>
  <p>I use <a href="http://www.ddj.com/cpp/184403758" rel="nofollow noreferrer">ScopeGuard</a> often in my code.  And one obvious use is to ensure that any temporary CDC objects are first selected into a given DC, then removed at scope exit, making my code leak free even under exceptional circumstances - and simultaneously cleaning up the code written (stupid multiple exit paths and try/catch and so on to try to handle removing any selected objects from a given CDC).</p>
  <p>So a more complete example of what I currently am forced to do looks like:</p>
  <pre><code>// get our client rect
  CRect rcClient;
  GetClientRect(rcClient);
  
  // get the real DC we're drawing on
  PAINTSTRUCT ps;
  CDC * pDrawContext = BeginPaint(&amp;ps);
  
  // create a drawing buffer
  CBitmap canvas;
  canvas.CreateCompatibleBitmap(pDrawContext, rcClient.Width(), rcClient.Height());
  
  CDC memdc;
  memdc.CreateCompatibleDC(pDrawContext);
  
  //*** HERE'S THE LINE THAT REALLY USES THE TYPEDEF WHICH i WISH TO ELIMINATE ***//
  ScopeGuard guard_canvas = MakeObjGuard(memdc, (SelectObjectBitmap)&amp;CDC::SelectObject, memdc.SelectObject(&amp;canvas));
  
  // copy the image to screen
  pDrawContext-&gt;BitBlt(rcClient.left, rcClient.top, rcClient.Width(), rcClient.Height(), &amp;memdc, rcClient.left, rcClient.top, SRCCOPY);
  
  // display updated
  EndPaint(&amp;ps);
  </code></pre>
  <p>It has always struck me as goofy as hell that I need to typedef every overloaded function which I wish to take the address of.</p>
  <p>So... is there a better way?!</p>
  <p><strong>EDIT: Based on the answers folks have supplied, I believe I have a solution to my underlying need: i.e. to have a more natural syntax for MakeGuard which deduces the correct SelectObject override for me:</strong></p>
  <pre><code>template &lt;class GDIObj&gt;
  ObjScopeGuardImpl1&lt;CDC, GDIObj*(CDC::*)(GDIObj*), GDIObj*&gt; MakeSelectObjectGuard(CDC &amp; dc, GDIObj * pGDIObj)
  {
      return ObjScopeGuardImpl1&lt;CDC, GDIObj*(CDC::*)(GDIObj*), GDIObj*&gt;::MakeObjGuard(dc, (GDIObj*(CDC::*)(GDIObj*))&amp;CDC::SelectObject, dc.SelectObject(pGDIObj));
  }
  </code></pre>
  <p>Which makes my above code change to:</p>
  <pre><code>ScopeGuard guard_canvas = MakeSelectObjectGuard(memdc, &amp;canvas);
  </code></pre>
  <p>//////////////////////////////////////////////////////////</p>
  <p>For those who might look here for a non-MFC version of the same thing:</p>
  <pre><code>//////////////////////////////////////////////////////////////////////////
  //
  // AutoSelectGDIObject
  //  selects a given HGDIOBJ into a given HDC,
  //  and automatically reverses the operation at scope exit
  //
  // AKA:
  //  &quot;Tired of tripping over the same stupid code year after year&quot;
  //
  // Example 1:
  //  CFont f;
  //  f.CreateIndirect(&amp;lf);
  //  AutoSelectGDIObject select_font(*pDC, f);
  //
  // Example 2:
  //  HFONT hf = ::CreateFontIndirect(&amp;lf);
  //  AutoSelectGDIObject select_font(hdc, hf);
  //
  // NOTE:
  //  Do NOT use this with an HREGION.  Those don't need to be swapped with what's in the DC.
  //////////////////////////////////////////////////////////////////////////
  
  class AutoSelectGDIObject
  {
  public:
      AutoSelectGDIObject(HDC hdc, HGDIOBJ gdiobj) 
          : m_hdc(hdc)
          , m_gdiobj(gdiobj)
          , m_oldobj(::SelectObject(m_hdc, gdiobj))
      {
          ASSERT(m_oldobj != m_gdiobj);
      }
  
      ~AutoSelectGDIObject()
      {
          VERIFY(m_gdiobj == ::SelectObject(m_hdc, m_oldobj));
      }
  
  private:
      const HDC       m_hdc;
      const HGDIOBJ   m_gdiobj;
      const HGDIOBJ   m_oldobj;
  };
  </code></pre>
  <p>//////////////////////////////////////////////////////////</p>
  <p><em><strong>Thanks Everyone who replied &amp; commented! :D</strong></em></p>