Live data from Hacker News

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

humanreadablemag.com

1–10 of 49 posts

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

#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".

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

#3
>> The first one is that if we overload the comma operator, we need to be extra careful to cover all cases and think about lvalues and rvalues. Otherwise we end up with buggy code.

Extra careful to cover all cases is when I start looking at alternatives. In this case it is not even enhancing readability. Even if you may be an excellent programmer think whether your team can be that extra careful most of the times before introducing these quirks in your code.

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

#4
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".

That sure is a weird usage

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

#5
post #4
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".

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...

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

#6
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".

In some countries (such as Germany), a comma is the official decimal separator -- so v += 1,5 will look quite innocent...

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

#7

>> The first one is that if we overload the comma operator, we need to be extra careful to cover all cases and think about lvalues and rvalues. Otherwise we end up with buggy code. Extra careful to cover all cases is when I start looking at alternatives. In this case it is not even enhancing readability. Even if you may be an excellent programmer think whether your team can be that extra careful most of the times bef…

“Extra careful to cover all cases is when I start looking at alternatives.“

IMO, an advantage of C++ is that, in well-written code, being extra careful is (mostly) limited to the implementer of code, not to its users.

It would be nice if nobody would have to be extra careful, but that’s the price you pay for power, I fear.

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

#8
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".

“there's not even an efficiency benefit from doing this. You'd have a more efficient and literate program with std::iota“

I would pick a different syntax, too, but this is more flexible than std::iota. You can do v+=a,b,c, for example.

I also would think any decent compiler would completely optimize away that appender instance, making it quite efficient for short lists of items (for longer lists, compiling a push_back call for each item would get inefficient, memory and cache-wise. I don’t see a compiler converting that into a loop)

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

#9
post #6
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".

In some countries (such as Germany), a comma is the official decimal separator -- so v += 1,5 will look quite innocent...

That's a valid point, but

v += 1.500

may be just as confusing.

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

#10
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".

I have to agree.

I never liked the comma operator, even in C. It's that kind of thing that "looks nice" at first but it is confusing in the end. It's not syntactic sugar, it's syntactic raisins.

v += [1, 2, 3, 4, 5] makes sense to me

or, for a less ambiguous meaning

v.extend([1, 2, 3, 4, 5]);

is just fine.

I don't think it makes sense to view comma as an operator.

Post reply on HN