By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,192 Members | 852 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,192 IT Pros & Developers. It's quick & easy.

std::size_t

P: n/a
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
Jul 22 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
"Christopher" <cp***@austin.rr.com> wrote...
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
Jul 22 '05 #2

P: n/a
Christopher wrote:
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).

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #3

P: n/a
Andrey Tarasevich wrote:
Christopher wrote:
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).

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

marc

Jul 22 '05 #4

P: n/a
"Marc Schellens" <m_*********@hotmail.com> wrote in message
news:3F**************@hotmail.com...
Andrey Tarasevich wrote:
Christopher wrote:
can someone refresh my meory on what std::size_t is used for? I am wonderingif my template container class should have it's capacity as a size_t or anint?

'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?


Because there's no clear requirement that all the elements of the
sequence controlled by a container have to fit in memory at the
same time.
And who says?
The C++ Standard suggests it by what it *doesn't* require of
containers, and what it *does* require of allocators.
And what to use instead?


allocator<T>::size_type, IIRC.

P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
Jul 22 '05 #5

P: n/a
Marc Schellens wrote:
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).


Why?


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.

--
Best regards,
Andrey Tarasevich

Jul 22 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.