std::unique_ptr and pointer to pointer

前端 未结 2 1331
悲&欢浪女
悲&欢浪女 2021-01-18 07:46

I want to use std::unique_ptr in combination with FreeImage\'s FITAG. The code in plain C would be:

... load image;

FITAG* tag = NULL;
FreeImag         


        
相关标签:
2条回答
  • 2021-01-18 07:49

    Reverse the order of operations; first acquire the resource and then construct the unique_ptr.

    FITAG *p = NULL;
    FreeImage_GetMetadata(FIMD_EXIF_EXIF, bitmap, "Property", &p);
    std::unique_ptr<FITAG, void(*)(FITAG*)> tag(p, &FreeImage_DeleteTag);
    
    0 讨论(0)
  • 2021-01-18 07:56
    tag.get()
    

    This returns an r-value. You can't get the address of an r-value( temporary ). Your best bet is to do this instead:

    auto ptr = tag.get();
    FreeImage_GetMetadata(FIMD_EXIF_EXIF, bitmap, "Property", &ptr);
    

    This will allow you to pass a FITAG** into the function. If you meant to pass a FITAG* into the function, just remove the & since tag.get() returns the pointer, not the value.

    FreeImage_GetMetadata(FIMD_EXIF_EXIF, bitmap, "Property", tag.get());
    
    0 讨论(0)
提交回复
热议问题