Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why shared_ptr<void> instead of shared_ptr<HANDLE>

Tags:

c++

boost

Based on http://en.highscore.de/cpp/boost/smartpointers.html#smartpointers_shared_pointer

#include <boost/shared_ptr.hpp> 
#include <windows.h> 

int main() 
{ 
  boost::shared_ptr<void> h(OpenProcess(PROCESS_SET_INFORMATION, FALSE, 
                  GetCurrentProcessId()), CloseHandle); 
  SetPriorityClass(h.get(), HIGH_PRIORITY_CLASS); 
}

Question:

Why the h is defined as boost::shared_ptr<void> rather than boost::shared_ptr<HANDLE>?

FYI:

WINBASEAPI
HANDLE
WINAPI
OpenProcess(
    __in DWORD dwDesiredAccess,
    __in BOOL bInheritHandle,
    __in DWORD dwProcessId
    );

typedef void * HANDLE;

http://www.boost.org/doc/libs/1_47_0/libs/smart_ptr/sp_techniques.html#pvoid

like image 987
q0987 Avatar asked Jan 14 '23 15:01

q0987


1 Answers

Because a boost::shared_ptr<HANDLE> would be a boost::shared_ptr<PVOID>, which is a boost::shared_ptr<void*> - which is, obviously, different than boost::shared_ptr<void>. Notice the extra pointer.

If you had a boost::shared_ptr<HANDLE>, it would essentially be a smart pointer to a pointer to void, as opposed to a smart pointer to void.

like image 115
Luchian Grigore Avatar answered Jan 27 '23 19:01

Luchian Grigore