0

The difference i wonder about between unique_ptr and shared_ptr in c++14 is shown below.

Example program (http://ideone.com/A4As5B):

#include <memory>

int main() {
    std::unique_ptr<unsigned char[]> ptr1 = std::unique_ptr<unsigned char[]>(new unsigned char[10]);
    std::shared_ptr<unsigned char[]> ptr2 = std::shared_ptr<unsigned char[]>(new unsigned char[10]);
    return 0;
}

Gives this compiler error:

Compilation error   time: 0 memory: 0 signal:0
In file included from /usr/include/c++/6/bits/shared_ptr.h:52:0,
                 from /usr/include/c++/6/memory:82,
                 from prog.cpp:2:
/usr/include/c++/6/bits/shared_ptr_base.h: In instantiation of ‘std::__shared_ptr<_Tp, _Lp>::__shared_ptr(_Tp1*) [with _Tp1 = unsigned char; _Tp = unsigned char []; __gnu_cxx::_Lock_policy _Lp = (__gnu_cxx::_Lock_policy)2u]’:
/usr/include/c++/6/bits/shared_ptr.h:117:32:   required from ‘std::shared_ptr<_Tp>::shared_ptr(_Tp1*) [with _Tp1 = unsigned char; _Tp = unsigned char []]’
prog.cpp:6:96:   required from here
/usr/include/c++/6/bits/shared_ptr_base.h:885:39: error: cannot convert ‘unsigned char*’ to ‘unsigned char (*)[]’ in initialization
         : _M_ptr(__p), _M_refcount(__p)
                                       ^

My question is, what could possibly be the reason why the unique_ptr syntax for array allocation made it into c++14 but for shared_ptr we have to wait until c++17? Are there any conceptual differences between the two smart pointers that makes this non-straight forward to specify for share_ptr when it is specified for unique_ptr?

Martin G
  • 17,357
  • 9
  • 82
  • 98
  • 2
    unique_ptr of array is available from C++11 – Danh Mar 22 '17 at 08:48
  • 1
    The people on the standards committee are just humans - they forget or miss stuff like everyone else. Just like `std::make_unique` was forgotten in C++11 and only added in C++14 while `std::make_shared` was added in C++11. – Jesper Juhl Mar 22 '17 at 09:19

0 Answers0