Skip to content

Commit

Permalink
Added shared_array constructor from nullptr, per #8894.
Browse files Browse the repository at this point in the history
  • Loading branch information
pdimov committed Dec 11, 2013
1 parent c103ace commit a41b81f
Show file tree
Hide file tree
Showing 3 changed files with 122 additions and 1 deletion.
8 changes: 8 additions & 0 deletions include/boost/smart_ptr/shared_array.hpp
Expand Up @@ -61,6 +61,14 @@ template<class T> class shared_array
{
}

#if !defined( BOOST_NO_CXX11_NULLPTR )

shared_array( boost::detail::sp_nullptr_t ) BOOST_NOEXCEPT : px( 0 ), pn()
{
}

#endif

template<class Y>
explicit shared_array( Y * p ): px( p ), pn( p, checked_array_deleter<Y>() )
{
Expand Down
3 changes: 2 additions & 1 deletion test/Jamfile.v2
@@ -1,6 +1,6 @@
# Boost.SmartPtr Library test Jamfile
#
# Copyright (c) 2003-2007 Peter Dimov
# Copyright (c) 2003-2013 Peter Dimov
# Copyright (c) 2003 Dave Abrahams
#
# Distributed under the Boost Software License, Version 1.0. (See
Expand Down Expand Up @@ -78,6 +78,7 @@ import testing ;
[ run sp_array_cast_test.cpp ]
[ run sp_zero_compare_test.cpp ]
[ run sp_nullptr_test.cpp ]
[ run sa_nullptr_test.cpp ]

[ compile-fail array_fail_spa_sp_c.cpp ]
[ compile-fail array_fail_sp_spa_c.cpp ]
Expand Down
112 changes: 112 additions & 0 deletions test/sa_nullptr_test.cpp
@@ -0,0 +1,112 @@
//
// shared_array nullptr test
//
// Copyright 2012, 2013 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0.
// See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt
//

#include <boost/shared_array.hpp>
#include <boost/detail/lightweight_test.hpp>
#include <cstddef>
#include <memory>

#if !defined( BOOST_NO_CXX11_NULLPTR )

struct X
{
static int instances;

X()
{
++instances;
}

~X()
{
--instances;
}

private:

X( X const & );
X & operator=( X const & );
};

int X::instances = 0;

int main()
{
{
boost::shared_array<int> p( nullptr );

BOOST_TEST( p.get() == 0 );
BOOST_TEST( p.use_count() == 0 );

BOOST_TEST( p == nullptr );
BOOST_TEST( nullptr == p );
BOOST_TEST( !( p != nullptr ) );
BOOST_TEST( !( nullptr != p ) );
}

{
boost::shared_array<int> p( new int[ 1 ] );

BOOST_TEST( p.get() != 0 );
BOOST_TEST( p.use_count() == 1 );

BOOST_TEST( p != nullptr );
BOOST_TEST( nullptr != p );
BOOST_TEST( !( p == nullptr ) );
BOOST_TEST( !( nullptr == p ) );

p = nullptr;

BOOST_TEST( p.get() == 0 );
BOOST_TEST( p.use_count() == 0 );

BOOST_TEST( p == nullptr );
BOOST_TEST( nullptr == p );
BOOST_TEST( !( p != nullptr ) );
BOOST_TEST( !( nullptr != p ) );
}

{
BOOST_TEST( X::instances == 0 );

boost::shared_array<X> p( new X[ 2 ] );
BOOST_TEST( X::instances == 2 );

BOOST_TEST( p.get() != 0 );
BOOST_TEST( p.use_count() == 1 );

BOOST_TEST( p != nullptr );
BOOST_TEST( nullptr != p );
BOOST_TEST( !( p == nullptr ) );
BOOST_TEST( !( nullptr == p ) );

p = nullptr;
BOOST_TEST( X::instances == 0 );

BOOST_TEST( p.get() == 0 );
BOOST_TEST( p.use_count() == 0 );

BOOST_TEST( p == nullptr );
BOOST_TEST( nullptr == p );
BOOST_TEST( !( p != nullptr ) );
BOOST_TEST( !( nullptr != p ) );
}

return boost::report_errors();
}

#else

int main()
{
return 0;
}

#endif

0 comments on commit a41b81f

Please sign in to comment.