size_type, pos_type, etc.

A

Alex J. Dam

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.
 
K

Kevin Goodsell

Alex said:
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.

-Kevin
 
C

Cy Edmunds

Alex J. Dam said:
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.

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

Hendrik Belitz

Cy said:
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.
 
R

Ron Natalie

Hendrik Belitz said:
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.
 
A

Alex J. Dam

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.
 
R

Ron Natalie

Alex J. Dam said:
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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,731
Messages
2,569,432
Members
44,835
Latest member
KetoRushACVBuy

Latest Threads

Top