Hacker News new | ask | show | jobs
by jeffbee 2209 days ago
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".

7 comments

It might be a bit extreme but the more I'm exposed to operator overloading the more I think that it's a bad idea 99% of the time.

The only use that seems absolutely in my opinion is when the overload is absolutely transparent mathematically, for instance to implement geometrical transformations on a Matrix class.

That works because in this case you don't actually end up with "custom" behavior, you just expand the standard and well understood notation of the language by plugging the standard and well understood mathematical notation for matrix operations. Anybody who understands this mathematical notation will be able to understand what the code does without additional context.

Anything beyond that is just asking for trouble IMO. It's basically code obfuscation.

Of course C++ has precedent for that sort of insanity, especially with the IMO absolutely bonkers use of the bit shift operators for... input/output processing. A notation that you'll note hasn't had a lot of success outside of C++.

> It might be a bit extreme but the more I'm exposed to operator overloading the more I think that it's a bad idea 99% of the time.

Quite a moderate position in practice. There are a substantial number of programmers who enjoy being able to tell what basic syntax does from memory without having to cross-reference documentation and source files.

And as you allude to, the example in question isn't a sum; it is an append. It would be far more appropriate to have an a = append(a, {1,2,3,4,5}) style construct. Or something with pointers if efficiency is important. Or construct the vector inline in older versions of C++. The += should be reserved for actual vector summing.

This whole article stands as a reminder that C++ is a remarkable language, and is starting to make up a lot of ground on achieving feature parity with Common Lisp.

I would add string + and += operators to list but that is it really.
That is pretty extreme. Where would we be without assignment and comparison overloads? Or dereference?
iostream hasn't had a lot of success inside C++ either. Operator overloading is one of those features that you wish for when you don't have it. Besides standard math operators, assignment also ends up playing a major role in making data structures behave well when managing resources.

It can be abused but most of the time is extremely convenient. Iostream and boost set a terrible example however. Boost especially goes into the deep end on templates, operators, crazy dependencies, unacceptable compile times etc. It really isn't fair to judge modern C++ on boost. Instead of being batteries included, boost is now more a last resort.

The "surprise" in the article is related to a very general class of mistake: supporting type something& but not also type something&& despite that parameter type being decisive for overload resolution purposes and despite the missing function being identical to the existing one.

Overloading the comma operator doesn't cause any particular danger, it merely provides a mildly unfavourable situation in which an unnecessary mistake is possible: there's "threat" of the default comma operator that does something completely different, it's not very obvious whether type something or something& or something&& is considered, and the involved overload resolution rules are somewhat lawyer-grade.

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.

A valid point. Nice.
Only if you forget whether you're reading German or C++.
As another example of unexpected behavior:

    a, b = 10, 20;
(This assigns 10 to b but leaves a untouched)
Not if you remember that the precedence of PROGN (“the comma operator”) is even lower than assignment.
What would you expect that to do instead? C++ doesn't have multiple assignment.
Well, the situation is even trickier:

   (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.
C++17 is capable of multiple variable declaration, which is a similar concept.

    #include <tuple>
    #include <iostream>
    
    std::tuple<int, int> divide(int dividend, int divisor) {
        return  {dividend / divisor, dividend % divisor};
    }
    
    int main() {
        using namespace std;
        auto [quotient, remainder] = divide(14, 3);
        cout << quotient << ',' << remainder << endl;
    }
Yes, the code

    auto [quotient, remainder] = divide(14, 3);
is not an assignment. In an assignment, you should write something like

    std::tie(quotient, remainder) = divide(14, 3);
which is a tuple assignment written as a single assignment.

This shows the kind of (imho) "ugly hacks" the C++ committee had to make to cover up the historical mistake with the comma operator.

Such misleading code should trigger an error.
Not really an error, since it is syntactically valid C++. It could definitely trigger a compiler warning, though.
Code that violates C++ static typing rules can be syntactically valid too, yet noone is proposing it should be only a warning.

(I find this the most weird thing about C/C++ culture: they are so proud it is static typed, yet oblivious to all the other problems and traps/memory unsafety/undefined behavior/etc.)

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.

Today you can use initilizer list to support this syntax:

  V+={1,2,3,4,5};
“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)

An optimizer can do many things until it cannot. Since you have no easy way to assert the overall performance of your program, you may end up with broken optimizations at some point in the future due to unrelated changes, pretty much non-deterministically.

In addition, more templates and more types means way less compiler throughput. That is why major parts of Boosts are avoided.

Not to mention human throughput too.

> compiling a push_back call for each item would get inefficient, memory and cache-wise?

That depends a lot on what you are doing.

By the way, if you are dealing with tons of constants in your code, then it usually means the design of the application is likely inflexible and a code smell overall.

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

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.
I didn't expect anyone to compliment Qt containers. Most of them are straight up not recommended for any situation because they waste memory, have unnecessary indirections that cause dcache misses, and aren't optimal for multithreading due to COW (and accidental COW due to bad APIs).
This was about ease of use and simplicity of implementation. By the way, QHash's performance will be fixed thoroughly in Qt6. It is significantly faster than std::unordered_map, whose performance is constrained by an API that effectively requires a sub-optimal implementation.

https://codereview.qt-project.org/c/qt/qtbase/+/287053

Yes, agreed, Qt is saner. Also the Borland C++ libraries were usually ok.
On the converse, why would you care about SOLID when implementing an array class? Not everything (or even most things) need to be OO.