473,761 Members | 3,651 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

size_type and size_t

Hello,

I think any specialized size_type, such as string::size_ty pe and
vector<T>::size _type, is defined in terms of size_t, which is much
more low level. Since they are more or less equivalent, what's the
advantage of using size_type over size_t?

Thanks,
Jess

May 22 '07 #1
3 2269
Jess wrote:
Hello,

I think any specialized size_type, such as string::size_ty pe and
vector<T>::size _type, is defined in terms of size_t, which is much
more low level. Since they are more or less equivalent, what's the
advantage of using size_type over size_t?

Thanks,
Jess
The problem is your premise, 'they are more or less equivalent', no,
they ain't equivalent at all. They are all highly
implementation/platform dependent.

F
May 22 '07 #2
Fei Liu wrote:
Jess wrote:
Hello,

I think any specialized size_type, such as string::size_ty pe and
vector<T>::size _type, is defined in terms of size_t, which is much
more low level. Since they are more or less equivalent, what's the
advantage of using size_type over size_t?
The problem is your premise, 'they are more or less equivalent', no,
they ain't equivalent at all. They are all highly
implementation/platform dependent.
No and yes. Here's what the standard says under

20 General utilities library

20.1.5 Allocator requirements

4 Implementations of containers described in this International
Standard are permitted to assume that their Allocator template
parameter meets the following two additional requirements beyond
those in Table 32.

— The typedef members pointer, const_pointer, size_type, and
difference_type are required to be T*,T const*, size_t, and
ptrdiff_t, respectively.
5 Implementors are encouraged to supply libraries that can accept
allocators that encapsulate more general memory models and that
support non-equal instances. In such implementations , any
requirements imposed on allocators by containers beyond those
requirements that appear in Table 32, and the semantics of
containers and algorithms when allocator instances compare
non-equal, are implementation-defined.


Brian

May 22 '07 #3
On May 22, 2:51 pm, Jess <w...@hotmail.c omwrote:
I think any specialized size_type, such as string::size_ty pe and
vector<T>::size _type, is defined in terms of size_t, which is much
more low level. Since they are more or less equivalent, what's the
advantage of using size_type over size_t?
In theory, or in practice?

In theory, vector and basic_string have an additional template
argument, which is an allocator. It defaults to the standard
allocator (so you don't have to specify it), and the standard
allocator is required to define size_type as size_t. But other,
user defined allocators can define it as something different.
(The size_type in vector and basic_string is just a typedef to
the type in the allocator.)

In practice, allocators are very subtle, not easy to use, and in
fact rarely used. Unless you're writing truly generic code, you
can generally ignore the issue, and just use size_t.

(I think that allocators were originally invented to support the
different types of pointers on a 16 bit Intel processor, so that
if e.g. you were compiling in small, with size_t a 16 bit
unsigned int, you could still declare a vector with an allocator
which used huge model, with 32 bit pointers and a 32 bit
unsigned long for size_type. In that context, they actually
make sense.)

--
James Kanze (GABI Software) email:ja******* **@gmail.com
Conseils en informatique orientée objet/
Beratung in objektorientier ter Datenverarbeitu ng
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

May 23 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2729
by: Fraser Ross | last post by:
If anyone has the book Effective STL do you agree that the word value should be type on P51, 2nd paragraph from the bottom, in the sentence starting 'In both cases'. allocator's size_type is required to be size_t so why say it is typically a typedef for size_t? Fraser.
10
8640
by: Grumble | last post by:
What is the difference between size_t and vector<T>::size_type? Are they ever a different type or a different size? Why should one type the latter when the former is shorter? What about the size_type type in other STL classes? -- Regards, Grumble
3
2399
by: Eric Lilja | last post by:
Hello, I was working on a program where I needed to take a vector storing objects of type std::string and convert each object to an int and store the ints in another vector. I decided that I should create a templated function for this because I've done similar things before and expect to do similar things again (i.e., converting a collection of one type to a collection of another type). This is my test program, it compiles but if I try to...
2
4474
by: asdf | last post by:
Who can tell the difference in detail?
13
7623
by: t.hall | last post by:
Is std::vector<T>::size_type guaranteed to be the same type as std::vector<U>::size_type? To be more explicit, given void f(T, U); and std::vector<Tvt; std::vector<Uvu; which have the same size, can I write
0
9522
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9336
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10111
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8770
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6603
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3866
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
3446
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.