Note that there are some explanatory texts on larger screens.

plurals
  1. POWhy does string::compare return an int?
    primarykey
    data
    text
    <p>Why does <a href="http://en.cppreference.com/w/cpp/string/basic_string/compare" rel="nofollow noreferrer"><code>string::compare</code></a> return an <code>int</code> instead of a smaller type like <code>short</code> or <code>char</code>? My understanding is that this method only returns -1, 0 or 1. </p> <p>Second part, if I was to design a compare method that compares two objects of type <code>Foo</code> and I only wanted to return -1, 0 or 1, would using <code>short</code> or <code>char</code> generally be a good idea?</p> <p>EDIT: I've been corrected, <code>string::compare</code> does not return -1, 0, or 1, it in fact returns a value >0, &lt;0 or 0. Thanks for keeping me in line guys.</p> <p>It seems like the answer is roughly, there is no reason to return a type smaller than <code>int</code> because return values are "rvalues" and those "rvalues" don't benefit from being smaller than type int (4 bytes). Also, many people pointed out that the registers of most systems are probably going to be of size <code>int</code> anyway, since these registers are going to be filled whether you give them a 1, 2 or 4 byte value, there is no real advantage to returning a smaller value.</p> <p>EDIT 2: In fact it looks like there may be extra processing overhead when using smaller datatypes such as alignment, masking, etc. The general consensus is that the smaller datatypes exist to conserve on memory when working with a lot of data, as in the case of an array. </p> <p>Learned something today, thanks again guys!</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload