'size_t' vs 'container::size_type'
Asked Answered
W

3

139

Is there is a difference between size_t and container::size_type?

What I understand is size_t is more generic and can be used for any size_types.

But is container::size_type optimized for specific kinds of containers?

Wilkey answered 27/5, 2009 at 23:56 Comment(0)
H
128

The standard containers define size_type as a typedef to Allocator::size_type (Allocator is a template parameter), which for std::allocator<T>::size_type is typically defined to be size_t (or a compatible type). So for the standard case, they are the same.

However, if you use a custom allocator a different underlying type could be used. So container::size_type is preferable for maximum generality.

Hood answered 28/5, 2009 at 0:0 Comment(5)
Can you clarify this answer? I have looked back at the draft standards as far back as N1804 and I don't see any relationship between Allocator::size_type and size_type. A quick glance at libstdc++ does not show anything similar to this either.Frankforter
@ShafikYaghmour, So this answer is slightly outdated, but to maximize portability I think the advice is still sound: C++03 specified "Table 32: size_type: a type that can represent the size of the largest object in the allocation model." At the time, size_t was the bet practical implementation of those constraints. However, in C++11, it is now defined essentially as: std::make_unsigned<X::difference_type>::type by default. Which in practice, will probably be the same or compatible with size_t.Hood
CARE the answer is incorrect.... see #4850178 TL:DR: allocators size_type need to be size_t and in C++17 size_type will be deprecated as is.Sceptre
@Sceptre I see nothing in that page, nor in the C++ 2017 Standard (23.10.8), to hint at size_type deprecation. What gives?Decretal
@Decretal see omegaup.com/docs/cpp/en/cpp/memory/allocator.htmlDesist
L
50
  • size_t is defined as the type used for the size of an object and is platform dependent.
  • container::size_type is the type that is used for the number of elements in the container and is container dependent.

All std containers use size_t as the size_type, but each independent library vendor chooses a type that it finds appropriate for its container.

If you look at , you'll find that the size_type of Qt containers is version dependent. In Qt3 it was unsigned int and in Qt4 it was changed to int.

Lamm answered 28/5, 2009 at 10:32 Comment(4)
I find it a bit odd to have the size of something expressed as an int. Could we ever have a negative size for a container?Cacuminal
@MihaiTodor: it's not unusual for people to use signed types for everything, I guess Qt is following suit. The reason is that mixed operations (in particular comparisons) are such a disaster area that many people would rather avoiding using unsigned types for numbers, than have to deal with and/or avoid mixed ops. Just because unsigned types can't express negative numbers, doesn't mean you have to use them for numbers that can't be negative :-) I confess I'm surprised it's int rather than ssize_t, int is kind of small.Correlation
"All std containers use size_t as the size_type" very very false and misleading. Yes, they USUALLY do ( at least all my compilers did that way ) but the C++ language reference doesen't state, that it need to be similar for all stl container!! so careSceptre
I think it is worth noting that A type whose size cannot be represented by std::size_t is ill-formedGuillemette
R
9

For std::[w]string, std::[w]string::size_type is equal to std::allocator<T>::size_type, which is equal to the std::size_t. For other containers, it's some implementation defined unsigned integer type.

Sometimes it's useful to have the exact type, so for example one knows where the type wraps around to (like, to UINT_MAX) so that one can make use of that. Or for templates, where you really need to pass two identical types to function/class templates.

Often i find i use size_t for brevity or iterators anyway. In generic code, since you generally don't know with what container instance your template is used and what size those containers have, you will have to use the Container::size_type typedef if you need to store the containers size.

Ralli answered 28/5, 2009 at 0:14 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.