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.)
t() function in R Language is used to calculate transpose of a matrix or Data Frame.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With