473,732 Members | 2,210 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

size_type, pos_type, etc.


Hi,

which guidelines should be used about those integer types defined in the
C++ Standard Library, like string::size_ty pe, istream::pos_ty pe, etc.? Are
they meant to be used only in the library code?

For example, if I call istream::tellg( ), should I store its result in a
variable of type pos_type?

Are there any problems if I simply use ints everywhere?

Thanks.

--
Alex J. Dam

Jul 22 '05 #1
6 5723
Alex J. Dam wrote:
Hi,

which guidelines should be used about those integer types defined in the
C++ Standard Library, like string::size_ty pe, istream::pos_ty pe, etc.? Are
they meant to be used only in the library code?
No, I don't think so.

For example, if I call istream::tellg( ), should I store its result in a
variable of type pos_type?
I'd recommend using the type that the library uses for a given purpose.
That way you are sure to have the necessary range, and comparisons will
be unsurprising.

Are there any problems if I simply use ints everywhere?
Yes. int is only required to store values up to 32,767. In common
implementations the size of a sequence or file can exceed that very
easily. Besides that, comparing signed and unsigned types can cause
problems, though this should only be an issue if the signed value is
negative, I think.

Thanks.


-Kevin
--
My email address is valid, but changes periodically.
To contact me please use the address from a recent posting.
Jul 22 '05 #2
"Alex J. Dam" <al******@yahoo .com.br> wrote in message
news:pa******** *************** *****@yahoo.com .br...

Hi,

which guidelines should be used about those integer types defined in the
C++ Standard Library, like string::size_ty pe, istream::pos_ty pe, etc.? Are
they meant to be used only in the library code?
No.

For example, if I call istream::tellg( ), should I store its result in a
variable of type pos_type?
Yes.

Are there any problems if I simply use ints everywhere?
You may get signed/unsigned warning messages depending on the compiler.

Thanks.

--
Alex J. Dam


If you want code which compiles cleanly across various compilers and
platforms you should use these types yourself.

--
Cy
http://home.rochester.rr.com/cyhome/
Jul 22 '05 #3
Cy Edmunds wrote:
Are there any problems if I simply use ints everywhere?


According to Josuttis excellent The C++ Standard Library book, you may get
undefined behaviour if you do not use the size_types of the containers,
e.g. using unsigned int instead of string::size_ty pe may not work if you
compare the value to string::npos.

--
To get my real email adress, remove the two onkas
--
Dipl.-Inform. Hendrik Belitz
Central Institute of Electronics
Research Center Juelich
Jul 22 '05 #4

"Hendrik Belitz" <ho************ **@fz-juelich.de> wrote in message news:bv******** **@zam602.zam.k fa-juelich.de...
Cy Edmunds wrote:
Are there any problems if I simply use ints everywhere?


According to Josuttis excellent The C++ Standard Library book, you may get
undefined behaviour if you do not use the size_types of the containers,
e.g. using unsigned int instead of string::size_ty pe may not work if you
compare the value to string::npos.


Implementation defined behavior.

unsigned int is the same size as size_type, in which case, it obviously works.
unsigned int is larger than size_type, in this case it can hold the npos value fine
and compare it later. Things work fine.
unsigned int is smaller than size_type. In this case npos will get truncated to
2**n -1 where n is the size in bits of unsigned int. This will not compare
equal to npos anymore. Of course, if you compare it to -1 (which is what
npos is defined to be converted to size_type), it will work fine.
Jul 22 '05 #5
On Mon, 26 Jan 2004 22:46:14 +0000, Kevin Goodsell wrote:
For example, if I call istream::tellg( ), should I store its result in a
variable of type pos_type?


I'd recommend using the type that the library uses for a given purpose.
That way you are sure to have the necessary range, and comparisons will
be unsurprising.


On my system, sizeof(pos_type ) is 12. It's not an integer, it's a class --
in fact, the standard says it should be a class. I tried to use the
operator ++ on a pos_type variable and the compiler refused my code.
Indeed, the class doesn't provides that operator.

Should I convert it to off_type, which is a true integer?

If I need to store the location of some data within a binary file, in that
same binary file, should I store the pos_type value returned by
ostream::teelp( )? (That would be strange. I would be storing extra
information, since I need only the position).

I'm confused as to what types should be used when calling Standard Library
functions, especially when mixing variables of different types, e.g., if I
divide an off_type by a size_t, what do I get?

Thanks for your replies.
Jul 22 '05 #6

"Alex J. Dam" <al******@yahoo .com.br> wrote in message news:pa******** *************** *****@yahoo.com .br...
On Mon, 26 Jan 2004 22:46:14 +0000, Kevin Goodsell wrote:
For example, if I call istream::tellg( ), should I store its result in a
variable of type pos_type?


I'd recommend using the type that the library uses for a given purpose.
That way you are sure to have the necessary range, and comparisons will
be unsurprising.


On my system, sizeof(pos_type ) is 12. It's not an integer, it's a class --
in fact, the standard says it should be a class.


The stream positions need to potentially store multibyte state information in
addition to the absolute file offset.

The standard containers (including strings) require the size/positions to be unsigned
integers of some flavors.

Jul 22 '05 #7

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

Similar topics

1
4971
by: Kevin Goodsell | last post by:
Can someone tell me what is required of the size_type type for standard containers (and other standard classes defining this type)? Specifically: 1) Are the requirements the same for each container & class? 2) Is it required to have a particular "signed-ness", and if so, which one? 3) Are there required minimum limits for size_type, and if so, what are they?
10
8634
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
2
5522
by: Rakesh Sinha | last post by:
Hello, I am writing this application for which I need to determine the file size. My code looks roughly as follows. #include <fstream> #include <iostream> using std::ifstream;
3
2397
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...
3
2268
by: Jess | last post by:
Hello, I think any specialized size_type, such as string::size_type 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
13
7618
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
3
2672
by: cablepuff | last post by:
Alright, I am sort of confused on c++ iterators having both distance_type and size_type. On sgi and Microsoft compiler it uses distance_type for input iterator. For mingw and cygwin it uses size_type , is it just standard issues or is it platform dependent.
15
1711
by: Lambda | last post by:
I'm writing a function to partition a vector according to a predicate. For example: vector<string>::size_type partition(vector<Student_info&v) { vector<string>::size_type i, j; i = -1; j = v.size(); while (true)
0
8946
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
8774
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
9447
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
9181
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8186
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
6031
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
4550
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
4809
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
3
2180
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.