dimanche 26 juin 2016

std::make_unique for GDI+ objects

I am trying to use std::unique_ptr for my gdi+ objects due to the benefits of automatic memory management.

Look at this standart gdi+ C style code:

Gdiplus::Graphics* graphics = Gdiplus::Graphics::FromImage(image);
graphics->DrawRectangle(/* parameters */); // Working

I know that I can use a std::unique_ptr as follows:

std::unique_ptr<Gdiplus::Graphics> graphics(Gdiplus::Graphics::FromImage(image));
graphics->DrawRectangle(/* parameters */); // Working

Now I want to construct the std::unique_ptr with std::make_unique but I can't get it to compile.

I have tried the following:

std::unique_ptr<Gdiplus::Graphics> graphics = std::make_unique<Gdiplus::Graphics>(Gdiplus::Graphics::FromImage(image));
graphics->DrawRectangle(/* parameters */); // Not working

But I get the following conversion error: C2664 Conversion of 1 argument from "Gdiplus::Graphics" in "HDC" is not possible.

I am using the latest version of VS2015.

I thought std::make_unique it should be used like this:

std::unique_ptr<T> varName = std::make_unique<T>(argument with which an instance of T will be constructed);

What am I doing wrong?

Aucun commentaire:

Enregistrer un commentaire