Boost logo

Boost Users :

Subject: Re: [Boost-users] boost::none for boost::shared_ptr
From: Ben Pope (benpope81_at_[hidden])
Date: 2014-01-30 02:12:00


On 30/01/14 14:23, Gavin Lambert wrote:
> Mere moments ago, quoth I:
>> On 30/01/2014 15:13, Quoth Ben Pope:
>>>> // invoke g with empty object
>>>> g(shared_ptr<CSomeReallyLongClassName>());
>>>
>>> g(0);
> [...]
>> (Also, the above doesn't compile in 1.53 because the raw pointer
>> constructor is marked explicit. Again, maybe that's been changed in the
>> interim.)

Whoops, You're right. I did test it, but I think I compiled it in C++11
mode!

> If you're using a C++11 compiler you should be able to use this though:
>
> g(nullptr);

Yes, of course. Internally boost::shared_ptr uses
boost::detail::sp_nullptr_t, which in C++11 is a typedef of
std::nullptr_t, there appears to be no alternative for C++98, which is a
shame.

I don't see why that type couldn't be defined to an empty struct in
C++98 mode.

Then, removing the #if !defined( BOOST_NO_CXX11_NULLPTR ) guard from the
constructor of shared_ptr that accepts a boost::detail::sp_nullptr_t
does indeed seem to work.

namespace boost {
    namespace detail {
       struct sp_nullptr_t {};
    }
    const detail::sp_nullptr_t nullptr = detail::sp_nullptr_t();
}

#include <boost/smart_ptr/shared_ptr.hpp>

int f(boost::shared_ptr<int> i) { return i ? *i : 0; }

int main()
{
    f(boost::nullptr);
}

...there's probably some reason I'm not thinking of.

Ben


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