c++: constructor template

Boris Kolpackov boris at kolpackov.net
Fri Apr 9 15:18:36 CDT 2004


Good day,


There is this interesting technique (or idiom, if you please) that
I found a really elegant example of. The code explains it:

  class auto_lock
  {
    template <typename x>
    static void
    unlock (void* p)
    {
      reinterpret_cast<x*> (p)->unlock ();
    }

  public:
    template <typename x>
    auto_lock (x& m)
      : m_ (reinterpret_cast<void*>(&m), unlock_ (&unlock<x>)
    {
      m.lock ();
    }

    ~auto_lock ()
    {
      unlock_ (m_);
    }

  private:
    void* m_;
    void (*unlock_) (void*);
  };



-boris
-------------- next part --------------
A non-text attachment was scrubbed...
Name: not available
Type: application/pgp-signature
Size: 652 bytes
Desc: Digital signature
Url : http://www.kolpackov.net/pipermail/notes/attachments/20040409/850687ef/attachment.bin


More information about the notes mailing list