News for hackers. Stuff that matters.
Re:C++23: The Next C++ Standard by AndrewStephens on Tuesday July 11, 2023 @01:38PM (#36684456)
> Fight me.

OK.

Operator overloading is a useful feature that saves a bunch of time and makes code way more readable.

You can quibble whether operator<<() is a good idea on streams and perhaps C++ takes the concept too far with operator,() but the basic idea makes a lot of sense.

string("hello ") + string("world"); complexNumber2 * complexNumber2; for (int i : std::views::iota(0, 6) | std::views::filter(even) | std::views::transform(square)) someSmartPtr->methodOnWrapperClass();

Re:C++23: The Next C++ Standard by coldpie on Tuesday July 11, 2023 @02:03PM (#36684814)
All the things you wrote could be about as easily written & much more easily read without operator overloading. Operator overloading only allows programmers to feel "smart" for doing a "clever" thing, to the detriment of future readers.

    string("hello ").append("world");

    complexNumber2.mult(complexNumber2);

    // wtf is even going on with this one in your example? have these people never heard of method chaining?
    for(int i : std::views::iota(0,6).filter(even).transform(square))
        (*smartPtr)->methodOnWrapperClass();
That's all about the same verbosity, it's much more clear to the reader even if they're unfamiliar with your codebase, and dropping operator overloading eliminates the "clever" option to do stupid crap like divide file path objects together.
Re:C++23: The Next C++ Standard by AndrewStephens on Tuesday July 11, 2023 @02:11PM (#36684945)
Would you advocate getting rid of operators altogether?

    3.times(2).plus(7)
Some things just lend themselves to being expressed in terms of simple operators.

    (*smartPtr)->methodOnWrapperClass();
That is still using the overloaded SmartPtr<>::operator*() method.

I understand the viewpoint that operator overload is syntactic sugar for things that can easily be done another way, I just disagree that costs outweigh the benefits.

Re:C++23: The Next C++ Standard by coldpie on Tuesday July 11, 2023 @02:15PM (#36685030)
> Would you advocate getting rid of operators altogether?

Of course not. It makes sense for built-in types, as everyone reading the code can be assumed to know them.

> That is still using the overloaded SmartPtr<>::operator*() method.

Good catch ;)

> I just disagree that costs outweigh the benefits.

Yah, I think that's the disagreement. My feeling is there's a teeny, tiny handful of appropriate places for it (almost entirely math) and it opens up a pandora's box of terrible decisions that programmers clearly find irresistible.

Re:C++23: The Next C++ Standard by JonChesterfield on Tuesday July 11, 2023 @08:18PM (#36688894)
Are you suggesting

  3.times(2).plus(7)
as a good thing or a bad thing? I see a.equals(b) occasionally from the first argument is magic crowd but 3.times is novel here. I'm really unsure what the order of operations is for that expression.
Re:C++23: The Next C++ Standard by gpderetta on Tuesday July 11, 2023 @05:57PM (#36687804)
C++ doesn't have extension methods, so you wouldn't be able to add custom algos to the set of chainable algos.

Overloading operator| is a crude way to get the equivalent of extension methods without having to change the language.

Re:C++23: The Next C++ Standard by fsloth on Tuesday July 11, 2023 @02:54PM (#36685621)
The majority of time in professional codebases is not spent on typing but reading and understanding code.

"saves a bunch of time and makes code way more readable"

Not when everybody defines their own operators.

Note - we are discussing operator overloading, not operators as features in syntax. Operators at the syntax level make life a lot easier. But then everybody uses the exactly same operator semantics, not some weird per-project abstraction.

The lines of code you wrote as an example are not saving anyones time, except when writing it if you are a slow typist and lack a proper IDE support for C++. If typing speed is an issue, get a better IDE, don't write terser code.

Re:C++23: The Next C++ Standard by jenadine on Tuesday July 11, 2023 @03:56PM (#36686462)
Hiding the actual logic in extra boilerplate doesn't make it easier to read.

The lines of code in the example save reader's time as they focus attention on the actual business logic.

Yes, this assumes that operator overloading follows some convention, but you need to follow conventions regardless to make readable code.

Re:C++23: The Next C++ Standard by lost_tourist on Tuesday July 11, 2023 @04:31PM (#36686841)
by this reasoning should we get rid of templates/generics as well?
Re:C++23: The Next C++ Standard by enriquto on Tuesday July 11, 2023 @05:07PM (#36687228)
YES
Re:C++23: The Next C++ Standard by woooooo on Tuesday July 11, 2023 @06:49PM (#36688281)
Making things explicit isn't necessarily boilerplate.
Re:C++23: The Next C++ Standard by joshuamorton on Tuesday July 11, 2023 @04:20PM (#36686709)
Code is read more often than written. Writing code that can be understood at a glance (by using common, well understood operators) optimizes for readability.

I think your argument is basically "people should not aggressively violate the implicit bonds of interfaces", which is true. But that goes for all interfaces, not just and not in particular those around operators.

We just have cases where it's common with operators because those are one of the few cases where we have lots of things that meet the interface and interact directly as opposed to hierarchically. The same kind of issue comes up with co/contravariant types and containers sometimes, but that's less often visible to end developers.

Re:C++23: The Next C++ Standard by cogman10 on Tuesday July 11, 2023 @06:46PM (#36688257)
I tend to agree with this. I like operator overloading for mathematical constructs (like complex numbers or even just for conversions of literal types, Imagine, for example, you have a gram type and a second type, if you said 1g / 1s you'd get 1gps, that seems reasonable)

I don't like it in the example given

    for (int i : std::views::iota(0, 6)
               | std::views::filter(even)
               | std::views::transform(square))
What benefit does this have over the Javay/Rusty version that looks like this

    for (int i : std::views::iota(0, 6)
               .filter(even)
               .transform(square))
?

No deducing what `|` means, you are applying the filter then transform function against the view.

Re:C++23: The Next C++ Standard by JonChesterfield on Tuesday July 11, 2023 @08:14PM (#36688867)
People don't use the same operator semantics. Is + commutative? Does it raise exceptions or sometimes return sentinels? What type conversions might == do?
Re:C++23: The Next C++ Standard by SubjectToChange on Wednesday July 12, 2023 @03:27AM (#36691414)
And how exactly do you propose library authors should work with user-defined types? Operator overloading is what allows algorithms to be efficiently generalized across types.
Re:C++23: The Next C++ Standard by throwaway894345 on Tuesday July 11, 2023 @03:46PM (#36686319)
The code isn't readable (you can't even reliably tell at a glance what the operator does) and it takes negligibly longer to write "add()" rather than "+" in your program (yes, 'add()' is more keystrokes and thus takes longer to type, but most of your program isn't addition instructions).

I think what people should advocate is full DSL capabilities with some unambiguous gate syntax so people know precisely that `foo * bar` is not using the host language syntax. Overloading operators is ambiguous and vastly incomplete (everyone is holding up matrix math as the shining example for the utility of operator overloading and you can't even express dot product notation in C++!)--it's a hack at best.

Re:C++23: The Next C++ Standard by josefx on Tuesday July 11, 2023 @05:48PM (#36687686)
> The code isn't readable (you can't even reliably tell at a glance what the operator does) and it takes negligibly longer to write "add()" rather than "+" in your program (yes, 'add()' is more keystrokes and thus takes longer to type, but most of your program isn't addition instructions).

Except now you replaced + with a name that tells you just as much/little as + does. So you made your program verbose for the sake of verbosity.

Re:C++23: The Next C++ Standard by throwaway894345 on Wednesday July 12, 2023 @08:06AM (#36693335)
No, you’ve made your program “verbose” (by a handful of characters) for the sake of clarity—there is no longer ambiguity about what code runs (of course, this assumes you aren’t similarly overloading named functions, which should also be disallowed).
Re:C++23: The Next C++ Standard by josefx on Wednesday July 12, 2023 @12:51PM (#36697548)
Given that your example uses neither namespacing nor explicit typing you seem to preach do as I say, not as I do.
Re:C++23: The Next C++ Standard by throwaway894345 on Wednesday July 12, 2023 @01:27PM (#36698260)
I don't know what example you're talking about. I haven't given an example in this thread. I think you may have me confused with another commenter.
Re:C++23: The Next C++ Standard by josefx on Wednesday July 12, 2023 @02:30PM (#36699501)
so that throwaway suggesting that add is barely longer than + wasn't you?
Re:C++23: The Next C++ Standard by throwaway894345 on Thursday July 13, 2023 @06:24AM (#36707076)
That was me, but I didn’t provide example code that would require namespaces. I don’t understand his your earlier comment makes sense in the context of this thread.
Re:C++23: The Next C++ Standard by JohnFen on Tuesday July 11, 2023 @04:44PM (#36686965)
Operator overloading is like spicy peppers. A little bit can make the program better, but it's very easy to use too much.
Re:C++23: The Next C++ Standard by cogman10 on Tuesday July 11, 2023 @06:47PM (#36688266)
Unfortunately, like spicy peppers, everyone's definition of "too much" is different. Some people are eating ghost chili peppers just fine while others are struggling with ketchup.
Re:C++23: The Next C++ Standard by Leo_Germond on Tuesday July 11, 2023 @11:59PM (#36690316)
Which is why we've banned spicy food: it's just too risky to be used safely.