<legend id='wHhJA'><style id='wHhJA'><dir id='wHhJA'><q id='wHhJA'></q></dir></style></legend>

<small id='wHhJA'></small><noframes id='wHhJA'>

<i id='wHhJA'><tr id='wHhJA'><dt id='wHhJA'><q id='wHhJA'><span id='wHhJA'><b id='wHhJA'><form id='wHhJA'><ins id='wHhJA'></ins><ul id='wHhJA'></ul><sub id='wHhJA'></sub></form><legend id='wHhJA'></legend><bdo id='wHhJA'><pre id='wHhJA'><center id='wHhJA'></center></pre></bdo></b><th id='wHhJA'></th></span></q></dt></tr></i><div id='wHhJA'><tfoot id='wHhJA'></tfoot><dl id='wHhJA'><fieldset id='wHhJA'></fieldset></dl></div>
    <bdo id='wHhJA'></bdo><ul id='wHhJA'></ul>

      1. <tfoot id='wHhJA'></tfoot>

      2. std::unique_ptr、删除器和 Win32 API

        时间:2023-08-01

        <small id='1xy2Q'></small><noframes id='1xy2Q'>

            <tbody id='1xy2Q'></tbody>

            <bdo id='1xy2Q'></bdo><ul id='1xy2Q'></ul>

            1. <i id='1xy2Q'><tr id='1xy2Q'><dt id='1xy2Q'><q id='1xy2Q'><span id='1xy2Q'><b id='1xy2Q'><form id='1xy2Q'><ins id='1xy2Q'></ins><ul id='1xy2Q'></ul><sub id='1xy2Q'></sub></form><legend id='1xy2Q'></legend><bdo id='1xy2Q'><pre id='1xy2Q'><center id='1xy2Q'></center></pre></bdo></b><th id='1xy2Q'></th></span></q></dt></tr></i><div id='1xy2Q'><tfoot id='1xy2Q'></tfoot><dl id='1xy2Q'><fieldset id='1xy2Q'></fieldset></dl></div>
              <tfoot id='1xy2Q'></tfoot>
              1. <legend id='1xy2Q'><style id='1xy2Q'><dir id='1xy2Q'><q id='1xy2Q'></q></dir></style></legend>
                • 本文介绍了std::unique_ptr、删除器和 Win32 API的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

                  问题描述

                  在 VC2012 中,我想使用唯一指针和删除器在构造函数中创建互斥锁,这样我就不需要创建析构函数来调用 CloseHandle.

                  In VC2012, I want to create a mutex in a constructor using a unique pointer and a deleter, so that I don't need to create a destructor just to call CloseHandle.

                  我原以为这会奏效:

                  struct foo
                  {
                      std::unique_ptr<HANDLE, BOOL(*)(HANDLE)> m_mutex;
                      foo() : m_mutex(CreateMutex(NULL, FALSE, NULL), CloseHandle) {}
                  }
                  

                  但在编译时出现错误:

                  error C2664: 'std::unique_ptr<_Ty,_Dx>::unique_ptr(void *,int 
                  (__cdecl *const &)(HANDLE)) throw()' : cannot convert parameter 1 from 
                  'HANDLE' to 'void *'
                  

                  当我这样修改构造函数时:

                  When I modify the constructor thus:

                  foo() : m_mutex((void*)CreateMutex(NULL, FALSE, 
                      (name + " buffer mutex").c_str()), CloseHandle) {}
                  

                  我得到了更不寻常的:

                  error C2664: 'std::unique_ptr<_Ty,_Dx>::unique_ptr(void *,
                  int (__cdecl *const &)(HANDLE)) throw()' : cannot convert 
                  parameter 1 from 'void *' to 'void *'
                  

                  我现在不知所措.HANDLE 是 void* 的 typedef:是否有一些我需要了解的转换魔法?

                  I'm at a loss now. HANDLE is a typedef for void*: is there some conversion magic I need to know about?

                  推荐答案

                  暂时忘记自定义删除器.当你说 std::unique_ptr 时,unique_ptr 构造函数期望接收一个 T*,但 CreateMutex> 返回一个 HANDLE,而不是一个 HANDLE *.

                  Forget about the custom deleter for now. When you say std::unique_ptr<T>, the unique_ptr constructor expects to receive a T*, but CreateMutex returns a HANDLE, not a HANDLE *.

                  有 3 种方法可以解决此问题:

                  There are 3 ways to fix this:

                  std::unique_ptr<void, deleter> m_mutex;
                  

                  您必须将 CreateMutex 的返回值转换为 void *.

                  You'll have to cast the return value of CreateMutex to a void *.

                  另一种方法是使用 std::remove_pointer 获取HANDLE 的底层类型.

                  Another way to do this is use std::remove_pointer to get to the HANDLE's underlying type.

                  std::unique_ptr<std::remove_pointer<HANDLE>::type, deleter> m_mutex;
                  

                  另一种方法是利用以下事实:如果 unique_ptr 的删除器包含名为 pointer 的嵌套类型,则 unique_ptrcode> 将使用该类型作为其托管对象指针而不是 T*.

                  Yet another way to do this is to exploit the fact that if the unique_ptr's deleter contains a nested type named pointer, then the unique_ptr will use that type for its managed object pointer instead of T*.

                  struct mutex_deleter {
                    void operator()( HANDLE h ) 
                    {
                      ::CloseHandle( h );
                    }
                    typedef HANDLE pointer;
                  };
                  std::unique_ptr<HANDLE, mutex_deleter> m_mutex;
                  foo() : m_mutex(::CreateMutex(NULL, FALSE, NULL), mutex_deleter()) {}
                  

                  <小时>

                  现在,如果要传递函数类型的指针作为删除器,那么在处理Windows API时还需要注意创建函数指针时的调用约定.


                  Now, if you want to pass a pointer to function type as the deleter, then when dealing with the Windows API you also need to pay attention to the calling convention when creating function pointers.

                  所以,一个函数指针指向 CloseHandle 必须像这样

                  So, a function pointer to CloseHandle must look like this

                  BOOL(WINAPI *)(HANDLE)
                  

                  综合起来,

                  std::unique_ptr<std::remove_pointer<HANDLE>::type, 
                                  BOOL(WINAPI *)(HANDLE)> m_mutex(::CreateMutex(NULL, FALSE, NULL),
                                                                  &::CloseHandle);
                  

                  我发现使用 lambda 更容易

                  I find it easier to use a lambda instead

                  std::unique_ptr<std::remove_pointer<HANDLE>::type, 
                                  void(*)( HANDLE )> m_mutex;
                  foo() : m_mutex(::CreateMutex(NULL, FALSE, NULL), 
                                  []( HANDLE h ) { ::CloseHandle( h ); }) {}
                  

                  或者按照@hjmd 在注释中的建议,使用 decltype 来推断函数指针的类型.

                  Or as suggested by @hjmd in the comments, use decltype to deduce the type of the function pointer.

                  std::unique_ptr<std::remove_pointer<HANDLE>::type, 
                                  decltype(&::CloseHandle)> m_mutex(::CreateMutex(NULL, FALSE, NULL),
                                                                    &::CloseHandle);
                  

                  这篇关于std::unique_ptr、删除器和 Win32 API的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持html5模板网!

                  上一篇:C++/Win32:如何等待挂起的删除完成 下一篇:拦截windows打开的文件

                  相关文章

                  最新文章

                • <i id='72hgY'><tr id='72hgY'><dt id='72hgY'><q id='72hgY'><span id='72hgY'><b id='72hgY'><form id='72hgY'><ins id='72hgY'></ins><ul id='72hgY'></ul><sub id='72hgY'></sub></form><legend id='72hgY'></legend><bdo id='72hgY'><pre id='72hgY'><center id='72hgY'></center></pre></bdo></b><th id='72hgY'></th></span></q></dt></tr></i><div id='72hgY'><tfoot id='72hgY'></tfoot><dl id='72hgY'><fieldset id='72hgY'></fieldset></dl></div>
                • <small id='72hgY'></small><noframes id='72hgY'>

                  1. <tfoot id='72hgY'></tfoot>

                    • <bdo id='72hgY'></bdo><ul id='72hgY'></ul>

                      <legend id='72hgY'><style id='72hgY'><dir id='72hgY'><q id='72hgY'></q></dir></style></legend>