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. unique_ptr and make_unique -- [SOLVED]

unique_ptr and make_unique -- [SOLVED]

Scheduled Pinned Locked Moved C / C++ / MFC
questionlinqfunctionalhelp
3 Posts 2 Posters 0 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.
  • Richard Andrew x64R Offline
    Richard Andrew x64R Offline
    Richard Andrew x64
    wrote on last edited by
    #1

    [See below the original question for the solution.] I have been struggling with this for a while now, and I can't seem to figure it out. Here's my declaration:

    std::unique\_ptr\>		\_VersionData{};
    

    And here's where I attempt to instantiate it:

    \_VersionData = std::make\_unique(new BYTE\[\_VersionDataLength\], \[\](BYTE\*\* ptr) {delete \*ptr; }); // Attempting to use a lambda expression as a custom destructor
    

    The errors I receive are:

    error C2440: 'initializing': cannot convert from 'initializer list' to 'unsigned char *'
    note: The initializer contains too many elements

    I'm probably way off in my interpretation of the documentation. Can someone show me how this is supposed to work? :) [SOLUTION] Turns out I was really way off with the attempt shown above. Here's the working code. The declaration in the class header file:

    std::unique\_ptr					\_VersionData;
    

    The instantiation in the source file:

    auto CustomDeleter = \[\](BYTE\* ptr) {delete\[\] ptr; };
    
    \_VersionData = std::unique\_ptr(new BYTE\[\_VersionDataLength\], CustomDeleter);
    

    And the most important part, the constructor initializer list:

    CFileVersionInfo::CFileVersionInfo()
    : _VersionData(nullptr, [](BYTE* ptr) {delete[] ptr; })
    {
    // constructor code here...
    }

    The difficult we do right away... ...the impossible takes slightly longer.

    L 1 Reply Last reply
    0
    • Richard Andrew x64R Richard Andrew x64

      [See below the original question for the solution.] I have been struggling with this for a while now, and I can't seem to figure it out. Here's my declaration:

      std::unique\_ptr\>		\_VersionData{};
      

      And here's where I attempt to instantiate it:

      \_VersionData = std::make\_unique(new BYTE\[\_VersionDataLength\], \[\](BYTE\*\* ptr) {delete \*ptr; }); // Attempting to use a lambda expression as a custom destructor
      

      The errors I receive are:

      error C2440: 'initializing': cannot convert from 'initializer list' to 'unsigned char *'
      note: The initializer contains too many elements

      I'm probably way off in my interpretation of the documentation. Can someone show me how this is supposed to work? :) [SOLUTION] Turns out I was really way off with the attempt shown above. Here's the working code. The declaration in the class header file:

      std::unique\_ptr					\_VersionData;
      

      The instantiation in the source file:

      auto CustomDeleter = \[\](BYTE\* ptr) {delete\[\] ptr; };
      
      \_VersionData = std::unique\_ptr(new BYTE\[\_VersionDataLength\], CustomDeleter);
      

      And the most important part, the constructor initializer list:

      CFileVersionInfo::CFileVersionInfo()
      : _VersionData(nullptr, [](BYTE* ptr) {delete[] ptr; })
      {
      // constructor code here...
      }

      The difficult we do right away... ...the impossible takes slightly longer.

      L Offline
      L Offline
      Lost User
      wrote on last edited by
      #2

      Now that you understand it, how about writing an article for the rest of us?

      Richard Andrew x64R 1 Reply Last reply
      0
      • L Lost User

        Now that you understand it, how about writing an article for the rest of us?

        Richard Andrew x64R Offline
        Richard Andrew x64R Offline
        Richard Andrew x64
        wrote on last edited by
        #3

        Thanks for the vote of confidence, Richard! :-D I can't say that I completely understand it. Getting it to work was partly just trying different things until I hit upon the magic combination! I do understand some parts of it better now, but hardly well enough to write an article. :)

        The difficult we do right away... ...the impossible takes slightly longer.

        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