[assignment] += for containers on right side

Hi! is it possible to make += also work for containers on right side? i.e. int a; std::vector<int> b; std::list<int> c; std::vector<int> d; d += a,b,c,d; this would be similar to strings where += at least works with one string on the right side. -Jochen

Hi Digi, as far as I know there is no such functionality in Boost. To implement this I would take inspirations from boost::assign and make use of boost::range. Regards, Kai On 7/19/12, Jochen Wilhelmy <jochen.wilhelmy@googlemail.com> wrote:
Hi!
is it possible to make += also work for containers on right side? i.e. int a; std::vector<int> b; std::list<int> c; std::vector<int> d; d += a,b,c,d;
this would be similar to strings where += at least works with one string on the right side.
-Jochen
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

std::valarray provides this type of functionality. If you need to use a vector, you can write your own operator overload: template<class T> std::vector<T> & operator += (std::vector<T> & l, int r) { std::for_each(l.begin(), l.end(), [r] (T & t) { t += r; }); return l; } Hope this helps. ________________________________ From: Kai Schroeder <kaischroeder3@googlemail.com> To: boost@lists.boost.org Sent: Friday, July 20, 2012 3:38 PM Subject: Re: [boost] [assignment] += for containers on right side Hi Digi, as far as I know there is no such functionality in Boost. To implement this I would take inspirations from boost::assign and make use of boost::range. Regards, Kai On 7/19/12, Jochen Wilhelmy <jochen.wilhelmy@googlemail.com> wrote:
Hi!
is it possible to make += also work for containers on right side? i.e. int a; std::vector<int> b; std::list<int> c; std::vector<int> d; d += a,b,c,d;
this would be similar to strings where += at least works with one string on the right side.
-Jochen
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost
_______________________________________________ Unsubscribe & other changes: http://lists.boost.org/mailman/listinfo.cgi/boost

On 19-07-2012 18:00, Jochen Wilhelmy wrote:
Hi!
is it possible to make += also work for containers on right side?
Many things are possible in C++.
i.e. int a; std::vector<int> b; std::list<int> c; std::vector<int> d; d += a,b,c,d;
this would be similar to strings where += at least works with one string on the right side.
You could maybe do push_back( d ).range( a ).range( b ).range( c ).range( d ); -Thorsten

On 19-07-2012 18:00, Jochen Wilhelmy wrote:
Hi!
is it possible to make += also work for containers on right side?
Many things are possible in C++.
Am 20.07.2012 15:19, schrieb Thorsten Ottosen: then why not do it ? ;-)
i.e. int a; std::vector<int> b; std::list<int> c; std::vector<int> d; d += a,b,c,d;
this would be similar to strings where += at least works with one string on the right side.
You could maybe do
push_back( d ).range( a ).range( b ).range( c ).range( 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); -Jochen

From: boost-bounces@lists.boost.org [mailto:boost-bounces@lists.boost.org] 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. Regards, Luke

On 19-07-2012 18:00, Jochen Wilhelmy wrote:
Hi!
is it possible to make += also work for containers on right side?
Many things are possible in C++.
i.e. int a; std::vector<int> b; std::list<int> c; std::vector<int> d; d += a,b,c,d;
this would be similar to strings where += at least works with one string on the right side.
You could maybe do push_back( d ).range( a ).range( b ).range( c ).range( d ); -Thorsten

On Thu, Jul 19, 2012 at 06:00:42PM +0200, Jochen Wilhelmy wrote:
Hi!
is it possible to make += also work for containers on right side? i.e. int a; std::vector<int> b; std::list<int> c; std::vector<int> d; d += a,b,c,d;
I would rather not have such a functionality, at least not without extra spelling around it. At a very minimum I'd expect some kind of d += a,expand_range(b),expand_range(c); or d += a,expand_ranges(b,c); or d += flatten(a,b,c); What if you have code that relies on an implicit conversion from those container types to the element type of the left hand side. You would suddenly and silently change the semantics of such code. Beware changes of semantics, because you _will_ have people that wittingly or unwittingly rely on them. -- Lars Viklund | zao@acc.umu.se
participants (6)
-
Amir Ansari
-
Jochen Wilhelmy
-
Kai Schroeder
-
Lars Viklund
-
Simonson, Lucanus J
-
Thorsten Ottosen