71
const std::string::size_type cols = greeting.size() + pad * 2 + 2;

Why string::size_type? int is supposed to work! it holds numbers!!!

0

4 Answers 4

118

A short holds numbers too. As does a signed char.

But none of those types are guaranteed to be large enough to represent the sizes of any strings.

string::size_type guarantees just that. It is a type that is big enough to represent the size of a string, no matter how big that string is.

For a simple example of why this is necessary, consider 64-bit platforms. An int is typically still 32 bit on those, but you have far more than 2^32 bytes of memory.

So if a (signed) int was used, you'd be unable to create strings larger than 2^31 characters. size_type will be a 64-bit value on those platforms however, so it can represent larger strings without a problem.

4
  • 1
    It's also the case on PowerPC and Cell. And, as far as I can recall, on Alpha as well. Plus, of course, I think x64 is the typical 64-bit CPU these days. ;) But you're right, it is obviously platform-dependant. Commented Jul 25, 2009 at 18:58
  • 3
    Which 64-bit Linux platform are we talking about here? On x64 machines, it still had 32-bit ints last I tried. And on Cell processors an int is also 32 bits. And by extension, I'm assuming the same to apply to Linux on PowerPC. So no, the Linux ABI varies from platform to platform, and most of the platforms I know of specify 4-bit ints, even on Linux. Commented Jul 26, 2009 at 22:30
  • 1
    But you're right. The hardware typically defines a common ABI that software should follow in order to allow interoperability. The OS defines an ABI which is usually identical, but might not be. And the compiler actually implements an ABI which again usually follows the OS, but doesn't strictly speaking have to. Commented Jul 26, 2009 at 22:31
  • And also for being machine independent. Commented Mar 9, 2016 at 15:33
26

The example that you've given,

const std::string::size_type cols = greeting.size() + pad * 2 + 2;

is from Accelerated C++ by Koenig. He also states the reason for his choice right after this, namely:

The std::string type defines size_type to be the name of the appropriate type for holding the number of characters in a string. Whenever we need a local variable to contain the size of a string, we should use std::string::size_type as the type of that variable.

The reason that we have given cols a type of std::string::size_type is to ensure that cols is capable of containing the number of characters in greeting, no matter how large that number might be. We could simply have said that cols has type int, and indeed, doing so would probably work. However, the value of cols depends on the size of the input to our program, and we have no control over how long that input might be. It is conceivable that someone might give our program a string so long that an int is insufficient to contain its length.

7

A nested size_type typedef is a requirement for STL-compatible containers (which std::string happens to be), so generic code can choose the correct integer type to represent sizes.

There's no point in using it in application code, a size_t is completely ok (int is not, because it's signed, and you'll get signed/unsigned comparison warnings).

3
  • 2
    Would one go as far as to say there's no point? Perhaps if you didn't want the most portable code it'd be okay to use size_t. Or for most practical situations today you can get away with size_t. But if there was no point to it, size_type wouldn't exist, now would it?
    – Steven Lu
    Commented Jul 3, 2013 at 1:10
  • 1
    size_t is guaranteed to be large enough, so at most you "waste" some space (in a register or on the stack) for using a larger-than-necessary value. These typedefs are for use in generic code, not when using std::string, which is a concrete model. That is, if you're in a template function taking an arbitrary basic_string, esp. with an arbitrary allocator, you should use the nested typedef. But yes, there's no point for std::string, because size_t is perfectly ok. Commented Feb 9, 2015 at 8:09
  • size_t is unsigned. As long as you compare with std::string::npos (as opposed to pos >= 0), you should be okay.
    – Jason
    Commented Sep 8, 2017 at 13:54
-1

size_t and std::string::size_type are the same types except for one important difference: Were they both will represent a value of any size, std::string::size_type (A member of std::string) uses a static constant value of -1 to represent npos. one past the end of the string. It tells the program that you have reached the end of the string. If you are using search, find, erase, replace, or any other modifying operations on strings, or writing your own then you may want to prefer std::string::size_type. If you are just iterating over the string size_t is probably fine.

Not the answer you're looking for? Browse other questions tagged or ask your own question.