Boost logo

Boost :

Subject: Re: [boost] [assignment] += for containers on right side
From: Simonson, Lucanus J (lucanus.j.simonson_at_[hidden])
Date: 2012-07-20 14:47:22

From: boost-bounces_at_[hidden] [mailto:boost-bounces_at_[hidden]] On Behalf Of Jochen Wilhelmy

>> Many things are possible in C++.
>then why not do it ? ;-)

That's a pretty scary statement. To use C++ well one has to learn and understand the many things that are possible and learn also not to do many of the many things that are possible.

>>> d += a,b,c,d;
>i'm after having to write only +=
>of course i can write insead of d += 1,2,3,4; d.push_back(1);d.push_back(2);d.push_back(3);d.push_back(4);

You can definitely use expression templates and overload the comma operator to get exactly the syntax you suggest. However, others may not appreciate your ascii art.

Clear and concise is good. Cryptic is bad. Words mean something, commas mean something else.

Consider that the following test program compiles without error:

#include <iostream>
int main(){
int d = 0;
d += 1,2,3,4;
std::cout << d << std::endl;
return 0;

and produces and output of 1.

If you change what comma means you stand a good chance of breaking code or astonishing people. The syntax you propose already has a meaning in the language, changing that meaning might even be considered "evil". ;)

Just to illustrate why it is evil, what happens when someone tries to write an initializer list:

vector<vector<int> > v;
v = {d+=a,b, c, d};

and doesn't get what they expected? It is actually a very bad idea, and that is why you shouldn't do it. You propose to break the language itself.


Boost list run by bdawes at, gregod at, cpdaniel at, john at