Closed yb303 closed 5 years ago
After a = 17; b = 5; a /= b; I expect a to be 3, not 17. Is there any harm in returning a reference?
Bad example.... I'll write a better not at 2am
Returning a reference would allow you to write code like this:
#include <iostream>
int main()
{
int a = 20;
int b = 2;
(a /= b) *= 2;
std::cout << a << std::endl;
}
Is there any harm in returning a reference?
No, but I have never seen anybody use such code because one can write the same code more elegantly using: a = (a / b) * 2
. But since both the Eigen and Armadillo C++ matrix libraries return a reference for /=
I have now also implemented this for libdivide https://github.com/ridiculousfish/libdivide/commit/d2082d1f06328b84586d8e2776c4a350d1dadaec.
Do you need this in your program? I am asking because I am not fully convinced this is a good idea even though the C++ standard returns a reference for /=. This feature is controversial as far as I know.