Boost logo

Boost Users :

Subject: Re: [Boost-users] Safe to pass dereferenced anonymous shared_ptr?
From: Chris Stankevitz (chrisstankevitz_at_[hidden])
Date: 2013-11-05 22:56:34


On Tue, Nov 5, 2013 at 3:30 PM, Krzysztof Czainski <1czajnik_at_[hidden]> wrote:
> In your (simplified I presume) example you don't share anything, you don't
> even need a pointer at all. In your real example you probably have some
> class with a user-defined constructor instead of int, but can't you do:

Kris, Scott, Edward,

Thank you for your replies regarding the safe use of the anonymous shared_ptrs.

===

Kris: FYI my more complex use-case looks like this:

class TCImage
{
  public:

    // Default constructs a black image
    TCImage(unsigned Width, unsigned Height);
};

boost::shared_ptr<const std::string> GetJpegBytes(const TCImage& Image);

GetJpegBytes generates JPEG bytes for an image and returns them as a
shared_ptr. The shared_ptr is helpful because these JPEG bytes are
passed around throughout my system.

My question arose when I had the following atypical special-case
(simplified but you get the idea):

void WriteJpegBytes(const std::string& JpegBytes);

void WriteBlackImage();

===

I considered three options:

1. Pass a dereferenced anonymous boost::shared_ptr

void WriteBlackImage()
{
  WriteJpegBytes(*TCJpegEncoder::GetBytes(TCImage(640, 480)));
}

2. Pass a dereferenced named boost::shared_ptr

void WriteBlackImage()
{
  boost::shared_ptr<const std::string> pBytes =
TCJpegEncoder::GetBytes(TCImage(640, 480));

  WriteJpegBytes(*pBytes);
}

3. Change my interface:

boost::shared_ptr<const std::string> GetJpegBytesAsSharedPointer(const
TCImage& Image);

std::string GetJpegBytesAsString(const TCImage& Image);

I opted for approach (1) but wanted it clear it first with you guys.

=====

Thank you again,

Chris


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net