Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>First, the specification is that it will return a value less than, equal to or greater than <code>0</code>, not necessarily <code>-1</code> or <code>1</code>. Secondly, return values are rvalues, subject to integral promotion, so there's no point in returning anything smaller.</p> <p>In C++ (as in C), every expression is either an rvalue or an lvalue. Historically, the terms refer to the fact that lvalues appear on the left of an assignment, where as rvalues can only appear on the right. Today, a simple approximation for non-class types is that an lvalue has an address in memory, an rvalue doesn't. Thus, you cannot take the address of an rvalue, and cv-qualifiers (which condition "access") don't apply. In C++ terms, an rvalue which doesn't have class type is a pure value, not an object. The return value of a function is an rvalue, unless it has reference type. (Non-class types which fit in a register will almost always be returned in a register, for example, rather than in memory.)</p> <p>For class types, the issues are a bit more complex, due to the fact that you <em>can</em> call member functions on an rvalue. This means that rvalues must in fact have addresses, for the <code>this</code> pointer, and can be cv-qualified, since the cv-qualification plays a role in overload resolution. Finally, C++11 introduces several new distinctions, in order to support rvalue references; these, too, are mainly applicable to class types.</p> <p>Integral promotion refers to the fact that when integral types smaller than an <code>int</code> are used as rvalues in an expression, in most contexts, they will be promoted to <code>int</code>. So even if I have a variable declared <code>short a, b;</code>, in the expression <code>a + b</code>, both <code>a</code> and <code>b</code> are promoted to <code>int</code> before the addition occurs. Similarly, if I write <code>a &lt; 0</code>, the comparison is done on the value of <code>a</code>, converted to an <code>int</code>. In practice, there are very few cases where this makes a difference, at least on 2's complements machines where integer arithmetic wraps (i.e. all but a very few exotics, today&mdash;I think the Unisys mainframes are the only exceptions left). Still, even on the more common machines:</p> <pre><code>short a = 1; std::cout &lt;&lt; sizeof( a ) &lt;&lt; std::endl; std::cout &lt;&lt; sizeof( a + 0 ) &lt;&lt; std::endl; </code></pre> <p>should give different results: the first is the equivalent of <code>sizeof( short )</code>, the second <code>sizeof( int )</code> (because of integral promotion).</p> <p>These two issues are formally orthogonal; rvalues and lvalues have nothing to do with integral promotion. <em>Except</em>... integral promotion only applies to rvalues, and most (but not all) of the cases where you would use an rvalue will result in integral promotion. For this reason, there is really no reason to return a numeric value in something smaller than <code>int</code>. There is even a very good reason not to return it as a character type. Overloaded operators, like <code>&lt;&lt;</code>, often behave differently for character types, so you only want to return characters as character types. (You might compare the difference:</p> <pre><code>char f() { return 'a'; } std::cout &lt;&lt; f() &lt;&lt; std::endl; // displays "a" std::cout &lt;&lt; f() + 0 &lt;&lt; std::endl; // displays "97" on my machine </code></pre> <p>The difference is that in the second case, the addition has caused integral promotion to occur, which results in a different overload of <code>&lt;&lt;</code> to be chosen.</p>
    singulars
    1. This table or related slice is empty.
    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.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      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