arraysc++11stdvectorunique-ptr

How to get array size stored in unique_ptr?


if I do:

std::unique_ptr<int[]> uparr(new int[1984]);

and I pass uparr to somebody without passing the 1984 to them can they see how many elements it has?
aka is there equivalent of vector's .size() for unique_ptr of array ?


Solution

  • No, there isn't. Dynamic arrays are a somewhat defective language feature. You'll essentially always want/need to pass the array length around separately (unless you have some kind of sentinel policy). So you might as well use std::vector (if you don't mind the extra word for the capacity).