ridiculousfish / libdivide

Official git repository for libdivide: optimized integer division
http://libdivide.com
Other
1.09k stars 77 forks source link

operator/= to return reference #51

Closed yb303 closed 5 years ago

kimwalisch commented 5 years ago

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.

yb303 commented 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?

yb303 commented 5 years ago

Bad example.... I'll write a better not at 2am

kimwalisch commented 5 years ago

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.