Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C / C++ / MFC
  4. how can use a non-static class member function as the custom deleter of std::unique_ptr?

how can use a non-static class member function as the custom deleter of std::unique_ptr?

Scheduled Pinned Locked Moved C / C++ / MFC
helpc++question
2 Posts 2 Posters 1 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • F Offline
    F Offline
    Falconapollo
    wrote on last edited by
    #1

    When I attempt to set custom deleter of std::unique_ptr, I got this error: .\src\iMoney.cpp(5): error C2664: 'std::unique_ptr<_Ty,_Dx>::unique_ptr(HINSTANCE__ *,void (__cdecl *const &)(HMODULE))' : cannot convert parameter 2 from 'void (__thiscall iMoney::* )(HMODULE)' to 'void (__cdecl *const &)(HMODULE)' 2> with 2> [ 2> _Ty=HMODULE, 2> _Dx=void (__cdecl *)(HMODULE) 2> ] 2> Reason: cannot convert from 'void (__thiscall iMoney::* )(HMODULE)' to 'void (__cdecl *const )(HMODULE)' 2> There is no context in which this conversion is possible 2> 2>Build FAILED. anybody can help me? Here is my code snippet:

    //this is my non-static class member variable
    std::unique_ptr m_ptrModule;

    void iMoney::freeLib(HMODULE hModule)//this is the non-static class member function
    {
    m_pThisApp->FreeLibrary(hModule);
    }

    //this my constructor
    iMoney::iMoney(CReader_App* pApp)
    : m_pThisApp(pApp)
    , m_ptrModule(nullptr, &iMoney::freeLib)
    {
    //hello money, i love you so much~
    }

    J 1 Reply Last reply
    0
    • F Falconapollo

      When I attempt to set custom deleter of std::unique_ptr, I got this error: .\src\iMoney.cpp(5): error C2664: 'std::unique_ptr<_Ty,_Dx>::unique_ptr(HINSTANCE__ *,void (__cdecl *const &)(HMODULE))' : cannot convert parameter 2 from 'void (__thiscall iMoney::* )(HMODULE)' to 'void (__cdecl *const &)(HMODULE)' 2> with 2> [ 2> _Ty=HMODULE, 2> _Dx=void (__cdecl *)(HMODULE) 2> ] 2> Reason: cannot convert from 'void (__thiscall iMoney::* )(HMODULE)' to 'void (__cdecl *const )(HMODULE)' 2> There is no context in which this conversion is possible 2> 2>Build FAILED. anybody can help me? Here is my code snippet:

      //this is my non-static class member variable
      std::unique_ptr m_ptrModule;

      void iMoney::freeLib(HMODULE hModule)//this is the non-static class member function
      {
      m_pThisApp->FreeLibrary(hModule);
      }

      //this my constructor
      iMoney::iMoney(CReader_App* pApp)
      : m_pThisApp(pApp)
      , m_ptrModule(nullptr, &iMoney::freeLib)
      {
      //hello money, i love you so much~
      }

      J Offline
      J Offline
      jschell
      wrote on last edited by
      #2

      I believe the delete method should take a iMoney not HMODULE (which is what the error says.) And since it takes an iMoney there is no reason for it to not be static.

      1 Reply Last reply
      0
      Reply
      • Reply as topic
      Log in to reply
      • Oldest to Newest
      • Newest to Oldest
      • Most Votes


      • Login

      • Don't have an account? Register

      • Login or register to search.
      • First post
        Last post
      0
      • Categories
      • Recent
      • Tags
      • Popular
      • World
      • Users
      • Groups