Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ?

like image 220
NoSenseEtAl Avatar asked Mar 26 '14 13:03

NoSenseEtAl


2 Answers

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).

like image 157
Kerrek SB Avatar answered Nov 09 '22 21:11

Kerrek SB


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.

like image 29
galop1n Avatar answered Nov 09 '22 19:11

galop1n