473,395 Members | 1,639 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

size_type, pos_type, etc.


Hi,

which guidelines should be used about those integer types defined in the
C++ Standard Library, like string::size_type, istream::pos_type, 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 5674
Alex J. Dam wrote:
Hi,

which guidelines should be used about those integer types defined in the
C++ Standard Library, like string::size_type, istream::pos_type, 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_type, istream::pos_type, 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_type 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.kfa-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_type 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
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...
10
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...
2
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
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...
3
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...
13
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...
3
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...
15
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 =...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
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...

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.