shared_ptr with non-pointer resources
===

In C++11 is it possible to use `shared_ptr` to control non-pointer resources?


----------


It is possible to use `unique_ptr` to manage non-pointer resources.  This is done by implementing a custom deleter class which provides:

1. A `typedef pointer {TYPE};` where `{TYPE}` is the non-pointer resource type
2. `operator()(pointer)` which frees the controlled resource

...and then instantiating a `unique_pointer` with the custom deleter as the second template parameter.

For example, under Windows it is possible to create a `unique_ptr` which manages a [service control handle][1].  This handle type is freed by calling `delete`, but by calling [`CloseServiceHandle()`][2].  Here is sample code which does this:

Custom Deleter
--------------

    struct SvcHandleDeleter
    {
    	typedef SC_HANDLE pointer;
    	SvcHandleDeleter() {};
    
    	template<class Other> SvcHandleDeleter(const Other&) {};
    
    	void operator()(SC_HANDLE h) const
    	{
    		CloseServiceHandle(h);
    	}
    };
    
    
    typedef std::unique_ptr<SC_HANDLE,SvcHandleDeleter> unique_sch;

Instantiation
-------------

    unique_sch scm(::OpenSCManagerA(0, 0, SC_MANAGER_ALL_ACCESS));


----------

Is it possible to use `shared_ptr` to control a non-pointer resource as well?

According to the documentation, there are [`shared_ptr` constructor overloads][3] which take provide the means to provide a custom deleter class, but none of the constructors accept a resource type that is not either a pointer or a wrapper around a pointer.

How can this be done?


  [1]: http://msdn.microsoft.com/en-us/library/windows/desktop/ms685104%28v=vs.85%29.aspx
  [2]: http://msdn.microsoft.com/en-us/library/windows/desktop/ms682028%28v=vs.85%29.aspx
  [3]: http://en.cppreference.com/w/cpp/memory/shared_ptr/shared_ptr