Boost logo

Boost Users :

Subject: Re: [Boost-users] Safe to pass dereferenced anonymous shared_ptr?
From: Krzysztof Czainski (1czajnik_at_[hidden])
Date: 2013-11-05 18:30:19


2013/11/6 Chris Stankevitz <chrisstankevitz_at_[hidden]>

> Q1: Is the below code dangerous?
> A1: Only if the anonymous shared_ptr is deleted before function f returns.
>
> Q2: Is the anonymous shared_ptr deleted before function f returns?
> A2: ?
>
> Thank you,
>
> Chris
>
> ===
>
> #include <iostream>
> #include <boost/make_shared.hpp>
>
> void f(const int& i)
> {
> std::cout << "i = " << i << std::endl;
> }
>
> int main()
> {
> f(*boost::make_shared<int>(42));
>
> return 0;
> }
>

I don't think this particular use is dangerous, but does it make sense? I
can't imagine why would you ever want to do that.

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:

f( int(42) );

HTH,
Kris



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