Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Can I use value_type on an instance of the vector, not on its type

Tags:

c++

sizeof

while playing and trying to calculate total size of vector I tried something like

vector<double> vd;
auto area = vd.size()* sizeof (vd::value_type); 
//Ive seen Stepanov use area as name for this kind of size, idk if he adds the sizeof vd also to area :)

Unfortunately this doesnt work... I need to use vector<double>::value_type but that makes code less readable. Can it be made to work? I dont like sizeof vd.front() because it just looks ugly to write front() for this.
EDIT: decltype variants also fit in what I would call ugly category...

like image 492
NoSenseEtAl Avatar asked Dec 18 '12 15:12

NoSenseEtAl


1 Answers

I think decltype can be used:

auto area = vd.size() * sizeof(decltype(vd)::value_type);

as you are using auto I assume C++11 is permitted.

Confirmed with g++ v4.7.2 and clang v3.3.

like image 53
hmjd Avatar answered Sep 22 '22 00:09

hmjd