c++ - Correct storage of container size on 32, 64 bit -


i converting application 64 bit. have occurrences of following pattern:

class someotherclass;  class someclass {     std::vector<someotherclass*> mlistofthings;      void somememberfunction (void)     {         // needs know size of member list variable         unsigned int listsize = mlistofthings.size();          // use listsize in further computations         //...     } } 

obviously in practical case not have more max_int items in list. wondered if there consensus 'best' way represent type.

each collection defines own return type size(), first approximation be:

std::vector<someotherclass*>::size_type listsize = mlistofthings.size() 

i assume correct, (personally) dont find 'easy reading', -1 clarity.

for c++011 aware compiler write

auto listsize = mlistofthings.size() 

which more readable.

so question, latter indeed best way handle storing container sizes in variable , using them in computations, regardless of underlying architecture (win32, win64, linux, macosx) ?

what want use matter of how "purist" want code be.

if you're on c++11, can use auto , done with.

otherwise, in extremely generic code (which designed work arbitrary allocators), can use container's nested typedef size_type. taken verbatim container's allocator.

in normal use of standard library containers, can use std::size_t. size_type used default allocators, , type guaranteed able store object size.

i wouldn't recommend using [unsigned] int, smaller necessary on 64-bit platforms (it's left @ 32 bits, although of course depends on compiler , settings). i've seen production code fail due unsigned int not being enough index container.


Comments

Popular posts from this blog

Android layout hidden on keyboard show -

google app engine - 403 Forbidden POST - Flask WTForms -

c - Why would PK11_GenerateRandom() return an error -8023? -