Does C++ R-Value has a size?

Viewed 106

Let's assume the largest number an int variable can hold is 10. Consider the following situation:

main()
{
int r1 = 10;
int r2 = 1;

int x = r1 + r2;
}

According to my current little knowledge, r1 + r2 expression creates a temporary variable to hold the result before copying that value to x.

What i want to know is since the largest x can hold is 10, i know (it's a guess actually) that if i print x, i get 10. But what about r1 + r2 ?. Does this temporary variable that represent the result of r1 + r2 expression also hold 10 ?.

In other words does this temporary variable also has a largest it can hold ?

This is probably a noob question and i apologise.

Please Note:

I asked this question based on what i thought what overflowing is. That is; i thought when a variable reach to a state where (let's say for an integer case), if i add one more integer to it's value it's gonna overflow. And i thought when that happens the maximum value it holds gonna stay the same regardless of me increasing it. But that's not the case apparently. The behaviour is undefined when overflow for most types. check @bolov's answer

2 Answers

Signed integers

Computing a value larger than the maximum value or smaller than the minimum value of an signed integer type is called "overflow" and is Undefined Behavior.

E.g.:

int a = std::numeric_limits<int>::max();
int b = 1;
a + b;

The above program has Undefined Behavior because the type of a + b is int and the value computed would overflow.

§ 8 Expressions [expr]

§ 8.1 Preamble [expr.pre]

  1. If during the evaluation of an expression, the result is not mathematically defined or not in the range of representable values for its type, the behavior is undefined.

Unsigned integers

Unsigned integers do not overflow because they are always computed in modulo arithmetic.

unsigned a = std::numeric_limits<unsigned>::max();
a + 1; // guaranteed to be 0

unsigned b = 0;
b - 1; // guaranteed to be std::numeric_limits<unsigned>::max();

§6.9.1 Fundamental types [basic.fundamental]

  1. Unsigned integers shall obey the laws of arithmetic modulo 2n where n is the number of bits in the value representation of that particular size of integer 49

49) This implies that unsigned arithmetic does not overflow because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type.

BTW, you cannot be sure, if it creates a new variable at all. I mean it all depends on the compiler, compiler options, etc. For instance, in some circumstances, the compiler can just calculate the value of the r-value (if it is vivid and possible for that point) and just put the calculated value into the variable.

For your example, it is obvious, that r1 + r2 == 11. Then the x might be constructed using a value 11. And this also doesn't mean, that the x will 100% constructed and a constructor will be called for him.

Once I debugged and saw, that a variable I declared (and defined) was not created at all (and also some calculations I had). That was because I didn't use the variable in any meaningful way and set the optimization to the highest level.

Related