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 ?
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).
No, the information is lost.
If you need to keep track of it, use a vector instead or if you really do not wants the extra services, write a small wrapper around an allocated array. You can for example look at the std::dynarray
proposal that was kicked off the c++1y standard.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With