
Andrei Alexandrescu (See Website for Email) wrote:
Just sent the message below to clc++m. Any comments are welcome.
There's a library that I understand is being proposed for Boost:
Actually, it's even already accepted: see the first entry of "Past Review Results" in http://boost-consulting.com/boost/more/formal_review_schedule.html
It does useful things, but it runs straight against the commonly given advice (with which I happen to agree) that one should not override operator, unless using expression templates that ensure left-to-right evaluation.
So now, in the referenced article, the author makes the comment: " Overloading operator comma is sometimes viewed as a bad practice [3]. However, it has been done with success in eg. the Generative Matrix Computation Library and Blitz to initialize matrices (see [4]) and [5]). The Initialization Library overloads the comma operator in a safe manner by letting set_cont and set_map return an object that is responsible for the initialization. Therefore it takes explicit action from the programmer to begin using the overloaded operator,(). "
I think the mentioned libraries do employ expression templates, while the initialization library does not.
IIRC, Blitz has the following syntax to initialize an array A = 1, 2, 3; and I believe it's almost the same which this library does, and I *guess* the above does not involve expression templates -- just overloaded operator, which pushes data to the array.
So I am unsatisfied. Anyone here can soothe my concerns? Is overloading the comma operator considered harmless after all?
I think in this context it's more or less harmless. One can do: vector<int> v; v += 1, 2, 3; but the += operator is only defined for specific containers, and it's not defined for vector by default. So there's little risk that user will inadvertently use it, meaning something else. - Volodya