std::size_t

C

Christopher

can someone refresh my meory on what std::size_t is used for? I am wondering
if my template container class should have it's capacity as a size_t or an
int?
,
Christopher
 
V

Victor Bazarov

Christopher said:
can someone refresh my meory on what std::size_t is used for? I am wondering
if my template container class should have it's capacity as a size_t or an
int?

std::size_t is a type of the value returned by 'sizeof' operator.
std::size_t is a type in which a size of an array is measured. It
is the type of the argument to the allocation function (used with
'new', for example). It comes from C, defined in <cstddef> header.

Victor
 
A

Andrey Tarasevich

Christopher said:
can someone refresh my meory on what std::size_t is used for? I am wondering
if my template container class should have it's capacity as a size_t or an
int?

'size_t' ('std::size_t') is a type that can be used to hold a size of
any object in C/C++ program. Since an array in C/C++ is an object
itself, this type can also be used to store the size of an array. But in
generic case the concepts of "object size" and "container size" are
orthogonal and this type should not be used to store the size of a
container (number of elements).
 
M

Marc Schellens

Andrey said:
'size_t' ('std::size_t') is a type that can be used to hold a size of
any object in C/C++ program. Since an array in C/C++ is an object
itself, this type can also be used to store the size of an array. But in
generic case the concepts of "object size" and "container size" are
orthogonal and this type should not be used to store the size of a
container (number of elements).


Why?
And who says?
And what to use instead?

marc
 
A

Andrey Tarasevich

Marc said:

It is rather obvious. Let me illustrate it by an example: when x86 was a
16 bit platform, certain C/C++ compilers used 16 bit unsigned integer
type as 'size_t'. That still didn't mean that numer of elements in
'std::list<>' was limited by 2^16. In that case the size of a single
elmement was limited by 2^16, but the maxumum length of the list was
only limited by the available memory.
And who says?

The important thing is that no one/nothing says that size of a container
should be limited by the range of 'size_t'.
And what to use instead?

Each container is allowed to have its own 'size_type'. If you are
designing a container, choose the appropriate type to represent is size.
If you are using a container, use its 'size_type' to hold the
container's size.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,054
Latest member
TrimKetoBoost

Latest Threads

Top