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