What is make_shared of unknown size?

In this answer state TC

boost::make_sharedetc. types of support arrays - either one of the unknown size or one fixed size

boost::shared_ptr<int[]> sh_arr2 = boost::make_shared<int[]>(30);
boost::shared_ptr<int[30]> sh_arr3 = boost::make_shared<int[30]>();

First, how does make_shared support an array type of unknown size? I would have thought that array size is not required.

Secondly, what is the difference between sh_arr2 and sh_arr3? It seems like they are creating an array from int 30 size.

+4
source share
2 answers

The example is not that big. An array of unknown size presumably means that it can be called as follows:

int arr2_size = 30;
boost::shared_ptr<int[]> sh_arr2 = boost::make_shared<int[]>(arr2_size);

arr2_size , "".

-, 30, sh_arr3 , , . .

+2

-, make_shared ? .

Boost shared_ptr::operator[](...). boost::detail::sp_extent, ( T T[]). http://www.boost.org/doc/libs/1_63_0/boost/smart_ptr/shared_ptr.hpp:

namespace boost{
template<typename T>
class shared_ptr{
   .....
    typename boost::detail::sp_array_access< T >::type operator[] ( std::ptrdiff_t i ) const
        {
            BOOST_ASSERT( px != 0 );
            BOOST_ASSERT( i >= 0 && ( i < boost::detail::sp_extent< T >::value || boost::detail::sp_extent< T >::value == 0 ) );

            return static_cast< typename boost::detail::sp_array_access< T >::type >( px[ i ] );
        }
    ....
}

namespace detail{

    template< class T > struct sp_array_access
    { typedef void type; };

    template< class T > struct sp_array_access< T[] >
    { typedef T & type; };

    template< class T, std::size_t N > struct sp_array_access< T[N] >
    { typedef T & type; };



    template< class T > struct sp_extent
    { enum _vt { value = 0 }; };

    template< class T, std::size_t N > struct sp_extent< T[N] >
    { enum _vt { value = N }; };
}//end namepace detail
}//end namespace boost

-, sh_arr2 sh_arr3? int 30.

. Boost docs:

1.53 Boost, shared_ptr . (T [] T [N]) . , T [] , T [N]; [] .

+1

Source: https://habr.com/ru/post/1667142/


All Articles