Live data from Hacker News

The Subtle Dangers of the Comma Operator (C++)

humanreadablemag.com

11–20 of 49 posts

Re: The Subtle Dangers of the Comma Operator (C++)

#12
post #4

Earlier quoted context omitted.

That sure is a weird usage

But it has a Boost package, which means someone thought it was reasonable! I'm intentionally using a weak argument here...

Reasonable and "C++ library" in the same line is almost an oxymoron.

We like to complain about Java but the C++ guys went all in and apparently can't seem to write a simple array without inheriting from at least 3 primitives and using a couple of templates.

Yes please tell me how you follow the "SOLID" principles when this is as frail as a house of cards

Re: The Subtle Dangers of the Comma Operator (C++)

#13
> Overloading the Comma Operator Is Powerful

Clearly, it's over 9000!

... more seriously though, overloading the comma operator is a bad idea and you shouldn't do it. In fact, almost nobody does it.

Specifically, you don't want to break the principle of least astonishment with a command such as

    v+= 1,2,3,4,5;
Assuming you want to add up a bunch of literals, what you could do is something like:

   std::array data { 1,2,3,4,5 };
   v += std::reduce(data.begin(), data.end());
and if you've implemented some and wrappers for containers, you would have something like an

    template
    typename Container::value_type 
    reduce(const Container& container);
and then you would write

   v += reduce(std::array{ 1,2,3,4,5 } );
which is terse and much clearer.

Re: The Subtle Dangers of the Comma Operator (C++)

#14

> Overloading the Comma Operator Is Powerful Clearly, it's over 9000! ... more seriously though, overloading the comma operator is a bad idea and you shouldn't do it. In fact, almost nobody does it. Specifically, you don't want to break the principle of least astonishment with a command such as v+= 1,2,3,4,5; Assuming you want to add up a bunch of literals, what you could do is something like: std::array data { 1,2,3…

An even more concise form is built in, and I think more obvious to readers:

    v.insert(v.end(), { 1, 2, 3, 4, 5 });

Re: The Subtle Dangers of the Comma Operator (C++)

#15

> Overloading the Comma Operator Is Powerful Clearly, it's over 9000! ... more seriously though, overloading the comma operator is a bad idea and you shouldn't do it. In fact, almost nobody does it. Specifically, you don't want to break the principle of least astonishment with a command such as v+= 1,2,3,4,5; Assuming you want to add up a bunch of literals, what you could do is something like: std::array data { 1,2,3…

[deleted]

Re: The Subtle Dangers of the Comma Operator (C++)

#16

Earlier quoted context omitted.

But it has a Boost package, which means someone thought it was reasonable! I'm intentionally using a weak argument here...

Reasonable and "C++ library" in the same line is almost an oxymoron. We like to complain about Java but the C++ guys went all in and apparently can't seem to write a simple array without inheriting from at least 3 primitives and using a couple of templates. Yes please tell me how you follow the "SOLID" principles when this is as frail as a house of cards

Qt is very reasonable and the standard library also isn't too bad. Boost (some of its sub-projects) is by far the worst in making simple things complicated.

Re: The Subtle Dangers of the Comma Operator (C++)

#17
post #2

The example (v += 1,2,3,4,5) is neither "nice" nor "expressive". It's confusing and dumb. The reader of this code can't be expected to know what it does. And, looking at the implementation, there's not even an efficiency benefit from doing this. You'd have a more efficient and literate program with std::iota. The rest of the article isn't wrong, but it fails to establish why anyone thinks this pattern is "nice".

As another example of unexpected behavior:

    a, b = 10, 20;
(This assigns 10 to b but leaves a untouched)

Re: The Subtle Dangers of the Comma Operator (C++)

#18
post #14

> Overloading the Comma Operator Is Powerful Clearly, it's over 9000! ... more seriously though, overloading the comma operator is a bad idea and you shouldn't do it. In fact, almost nobody does it. Specifically, you don't want to break the principle of least astonishment with a command such as v+= 1,2,3,4,5; Assuming you want to add up a bunch of literals, what you could do is something like: std::array data { 1,2,3…

An even more concise form is built in, and I think more obvious to readers: v.insert(v.end(), { 1, 2, 3, 4, 5 });

I was actually not describing adding elements to a container, but adding up values.

Re: The Subtle Dangers of the Comma Operator (C++)

#19

Earlier quoted context omitted.

Reasonable and "C++ library" in the same line is almost an oxymoron. We like to complain about Java but the C++ guys went all in and apparently can't seem to write a simple array without inheriting from at least 3 primitives and using a couple of templates. Yes please tell me how you follow the "SOLID" principles when this is as frail as a house of cards

Qt is very reasonable and the standard library also isn't too bad. Boost (some of its sub-projects) is by far the worst in making simple things complicated.

Yes, agreed, Qt is saner. Also the Borland C++ libraries were usually ok.

Re: The Subtle Dangers of the Comma Operator (C++)

#20
post #17
post #2

The example (v += 1,2,3,4,5) is neither "nice" nor "expressive". It's confusing and dumb. The reader of this code can't be expected to know what it does. And, looking at the implementation, there's not even an efficiency benefit from doing this. You'd have a more efficient and literate program with std::iota. The rest of the article isn't wrong, but it fails to establish why anyone thinks this pattern is "nice".

As another example of unexpected behavior: a, b = 10, 20; (This assigns 10 to b but leaves a untouched)

What would you expect that to do instead? C++ doesn't have multiple assignment.
Post reply on HN