Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is T() = T() allowed?

I believe the expression T() creates an rvalue (by the Standard). However, the following code compiles (at least on gcc4.0):

class T {};

int main()
{
    T() = T();
}

I know technically this is possible because member functions can be invoked on temporaries and the above is just invoking the operator= on the rvalue temporary created from the first T().

But conceptually this is like assigning a new value to an rvalue. Is there a good reason why this is allowed?

Edit: The reason I find this odd is it's strictly forbidden on built-in types yet allowed on user-defined types. For example, int(2) = int(3) won't compile because that is an "invalid lvalue in assignment".

So I guess the real question is, was this somewhat inconsistent behavior built into the language for a reason? Or is it there for some historical reason? (E.g it would be conceptually more sound to allow only const member functions to be invoked on rvalue expressions, but that cannot be done because that might break some existing code.)

like image 373
Rimo Avatar asked May 28 '10 04:05

Rimo


People also ask

What does T () do in R?

t() function in R Language is used to calculate transpose of a matrix or Data Frame.

What is a T T in Matrix?

The transpose of a matrix is found by interchanging its rows into columns or columns into rows. The transpose of the matrix is denoted by using the letter “T” in the superscript of the given matrix. For example, if “A” is the given matrix, then the transpose of the matrix is represented by A' or AT.


1 Answers

This is allowed purely because of operator overloading, and the possibility that you may overloaded the operator = to do something more fancy, like print to the console, or lock a mutex, or anything really.

like image 170
Inverse Avatar answered Oct 01 '22 14:10

Inverse