Earlier quoted context omitted.
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.
The Subtle Dangers of the Comma Operator (C++)
21–30 of 49 posts
Re: The Subtle Dangers of the Comma Operator (C++)
#22Earlier 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
Re: The Subtle Dangers of the Comma Operator (C++)
#23One thing I like about the C (not ++) language is that it's always clear what function that is being called by looking at the function name. In C++ you have to guess a lot of times (or look very carefully).
Re: The Subtle Dangers of the Comma Operator (C++)
#24The 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.
V+={1,2,3,4,5};Re: The Subtle Dangers of the Comma Operator (C++)
#25Nope... std::vector::insert() takes an std::initializer_list [1].
[1] https://en.cppreference.com/w/cpp/container/vector/insert
Re: The Subtle Dangers of the Comma Operator (C++)
#26Re: The Subtle Dangers of the Comma Operator (C++)
#27I would rather call this "the subtle dangers of function overloading". One thing I like about the C (not ++) language is that it's always clear what function that is being called by looking at the function name. In C++ you have to guess a lot of times (or look very carefully).
Re: The Subtle Dangers of the Comma Operator (C++)
#28Earlier quoted context omitted.
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.
#include
#include
std::tuple divide(int dividend, int divisor) {
return {dividend / divisor, dividend % divisor};
}
int main() {
using namespace std;
auto [quotient, remainder] = divide(14, 3);
cout Re: The Subtle Dangers of the Comma Operator (C++)
#29Earlier quoted context omitted.
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.
(a, b) = (10, 20)
is allowed code in C++ (not in C). The result is that 20 is assigned to b, and a is untouched.Contrast this to:
a, b = 10, 20
where (as above) 10 is assigned to b and a is left untouched, because it is parsed as: a, (b = 10), 20
and I hope you agree that the behavior is very confusing.