Why pre-increment operator gives rvalue in C?

C doesn’t have references. In C++ ++i returns a reference to i (lvalue) whereas in C it returns a copy(incremented). C99 6.5.3.1/2 The value of the operand of the prefix ++ operator is incremented. The result is the new value of the operand after incrementation. The expression ++Eis equivalent to (E+=1). ‘‘value of an expression’’ … Read more

Taking the address of a temporary object

The word “shall” in the standard language means a strict requirement. So, yes, your code is ill-formed (it is an error) because it attempts to apply address-of operator to a non-lvalue. However, the problem here is not an attempt of taking address of a temporary. The problem is, again, taking address of a non-lvalue. Temporary … Read more

Are all temporaries rvalues in C++?

No. The C++ language specification never makes such a straightforward assertion as the one you are asking about. It doesn’t say anywhere in the language standard that “all temporary objects are rvalues”. Moreover, the question itself is a bit of misnomer, since the property of being an rvalue in the C++ language is not a … Read more

Do rvalue references allow dangling references?

Do rvalue references allow dangling references? If you meant “Is it possible to create dangling rvalue references” then the answer is yes. Your example, however, string middle_name () { return “Jaan”; } int main() { string&& nodanger = middle_name(); // OK. // The life-time of the temporary is extended // to the life-time of the … Read more

Why “universal references” have the same syntax as rvalue references?

I think it happened the other way around. The initial idea was to introduce rvalue-references into the language, meaning that “the code providing the double-ampersand reference does not care about what will happen to the referred-to object”. This permits move semantics. This is nice. Now. The standard forbids constructing a reference to a reference, but … Read more

tech