News for hackers. Stuff that matters.
Re:C++23: The Next C++ Standard by spacechild1 on Tuesday July 11, 2023 @02:36PM (#36685347)
Sigh. Do you really think that "(vec1.mul(vec2)).plus(vec3.mul(vec4))" is better than "vec1 * vec2 + vec3 * vec4"? Thanks, but no thanks.
Re:C++23: The Next C++ Standard by coldpie on Tuesday July 11, 2023 @02:42PM (#36685449)
Try this on:

    plus(
        mul(vec1, vec2),
        mul(vec3, vec4))
Yeah it's a tiny bit clumsier, and prefix notation takes some getting used to. But on the plus side we avoid all the too-clever travesties programmers have inflicted on us with bad operator overloading decisions! On the whole I think it's easily worth the trade.
Re:C++23: The Next C++ Standard by spacechild1 on Tuesday July 11, 2023 @02:50PM (#36685574)
Again, no thanks. I want mathematical notation and I simply won't use any language without operator overloading. Free functions for common mathematical operations are an abomination.
Re:C++23: The Next C++ Standard by throwaway894345 on Tuesday July 11, 2023 @03:29PM (#36686096)
Then you should probably use a language that lets you write DSLs for any given domain, rather than abusing operator overloading which just happens to work for a few subdomains of mathematics (e.g., you can't use mathematical conventions for dot product multiplication in C++). Anyway, I've never seen any bugs because someone misunderstood what a `mul()` function does, but I've definitely seen bugs because they didn't know that an operator was overloaded (spooky action at a distance vibes).
Re:C++23: The Next C++ Standard by pjmlp on Tuesday July 11, 2023 @04:31PM (#36686835)
Assuming mul does actually a multiplication.
Re:C++23: The Next C++ Standard by throwaway894345 on Wednesday July 12, 2023 @08:11AM (#36693378)
It doesn’t matter, you can look it up easily because without overloading, you know exactly which implementation will run.
Re:C++23: The Next C++ Standard by pjmlp on Wednesday July 12, 2023 @10:10AM (#36694816)
Ah, just like I do when I press F12 over an operator.
Re:C++23: The Next C++ Standard by enriquto on Wednesday July 12, 2023 @11:04AM (#36695608)
You come across as a bit facetious here.

Of course, the compiler or an advanced IDE can know what your code means. If all your identifiers were random permutations of l and I: lIllI1lI, your IDE would not mind either, but the code would be horrific, don't you agree? The point of the OP is that overloaded operators (and functions) make it harder to reason about the code for a human that reads it. At least for some people. At the end, everything is "just" syntactic sugar, but it makes a significant difference.

Re:C++23: The Next C++ Standard by throwaway894345 on Wednesday July 12, 2023 @02:19PM (#36699277)
Exactly. If you don't care that the code is unreadable and you can rely on every human viewing the code through an IDE with symbol resolution (and not say, online code review platforms) and remembering to use said symbol resolution to check every operator, then operator overloading is great!
Re:C++23: The Next C++ Standard by spacechild1 on Tuesday July 11, 2023 @04:31PM (#36686838)
Actually, I'm quite happy what C++ has to offer :)

Yes, the * operator can be ambiguous in the context of classic vector math (although that is just a matter of documentation), but not so much with SIMD vectors, audio vectors, etc.

Again:

a) vec4 = (vec1 - vec2) * 0.5 + vec3 * 0.3;

or

b) vec4 = plus(mul(minus(vec1, vec2), 0.5), mul(vec3, 0.3));

Which one is more readable? That's pretty much the perfect use case for operator overloading.

Re:C++23: The Next C++ Standard by spacechild1 on Tuesday July 11, 2023 @05:04PM (#36687193)
Regarding the * operator, I think glm got it right: * is element-wise multiplication, making it consistent with the +,-,/ operators; dot-product and cross-product are done with dedicated free functions (glm::dot and glm::cross).
Re:C++23: The Next C++ Standard by cuteboy19 on Wednesday July 12, 2023 @12:40AM (#36690532)
That's what numpy does too.
Re:C++23: The Next C++ Standard by fpoling on Tuesday July 11, 2023 @06:58PM (#36688352)
One never writes such expression in a serious code. Even with move semantic and lazy evaluation proxies it is hard to avoid unnecessary copies. Explicit temporaries make code mode readable and performant:

auto t = minus(vec1, vec2); mul_by(t, 0.5/0.3); add(t, vec3); mul_by(t, 0.3); v4 = std::move(t);

Re:C++23: The Next C++ Standard by Tunabrain on Tuesday July 11, 2023 @08:07PM (#36688810)
I think there may be a misunderstanding here regarding the use case. If the vectors are large and allocated on the heap/on an accelerator, then yes, writing out explicit temporaries may be faster. Of course, this does not preclude operator overloading at all: You could write the same code as auto t = vec1 - vec2; t *= 0.5/0.3; t += vec3; t *= 0.3;

However, if the operands are small (e.g. 2/3/4 element vectors are very common), then "unnecessary copies" or move semantics don't come into play at all. These are value types and the compiler would boil them down to the same assembly as the code you post above. Many modern C++ codebases in scientific computing, rendering, or the game industry make use of vector classes with operator overloading, with no performance drawbacks whatsoever; however, code is much more readable, as it matches actual mathematical notation.

Re:C++23: The Next C++ Standard by spacechild1 on Tuesday July 11, 2023 @08:25PM (#36688945)
Thank you for putting this so eloquently!

> Many modern C++ codebases in scientific computing, rendering, or the game industry make use of vector classes with operator overloading, with no performance drawbacks whatsoever

I guess these people are all not writing "serious code" :-p

Re:C++23: The Next C++ Standard by spacechild1 on Tuesday July 11, 2023 @08:15PM (#36688876)
> One never writes such expression in a serious code.

Oh please, because you know exactly which kind of code I write? I'm pretty sure that with glm::vec3 the compiler can optimize this just fine. Also, "vec" could really be anything, it is just a placeholder.

That being said, if you need to break up your statements, you can do so with operators:

    auto t = vec1 - vec2;
    t *= 0.5/0.3;
    t += vec3;
    t *= 0.3;
Personally, I find this much more readable. But hey, apparently there are people who really prefer free functions. I accept that.
Re:C++23: The Next C++ Standard by _gabe_ on Wednesday July 12, 2023 @12:27AM (#36690476)
TIL Box2D must not be serious code because it doesn't use copious amounts of explicit temporaries[0].

And just for the record, I'm very glad Erin Catto decided to use operator overloading in his code. It made it much easier for me to read and understand what the code was doing as opposed to it being overly verbose and noisy.

[0]: https://github.com/erincatto/box2d/blob/main/src/collision/b...

Re:C++23: The Next C++ Standard by MadcapJake on Tuesday July 11, 2023 @04:31PM (#36686839)
If editors were to implement it, you could navigate to the corresponding overload implementation or even provide some hint text. Just like they do for other functions.
Re:C++23: The Next C++ Standard by pjmlp on Wednesday July 12, 2023 @03:57AM (#36691609)
In Visual Studio it is already there.
Re:C++23: The Next C++ Standard by throwaway894345 on Wednesday July 12, 2023 @02:22PM (#36699348)
Yeah, we would need editors and code review tools to not only follow overloads to their functions but also highlight that the operator is overloaded in the first place. Of course, this is quite a lot more work than just not overloading things in the first place (particularly since the benefit of operator overloading is negligible).
Re:C++23: The Next C++ Standard by eesmith on Tuesday July 11, 2023 @05:42PM (#36687627)
Dealing with money is important, even if it's only a small part of mathematics. I'll focus on that.

Python's 'decimal' module uses overloaded operators so you can do things like:

  from decimal import Decimal as D

  tax_rate = D('0.0765')

  subtotal = 0
  for item in purchase:
     subtotal += item.price * item.count # assume price is a Decimal
  
  taxes = (subtotal * tax_rate).quantize(D('0.00'))
  total = subtotal + taxes
Plus, there's support for different rounding modes and precision. In Python's case, something like "a / b" will look to a thread-specific context which specifies the appropriate settings:

  >>> import decimal
  >>> from decimal import localcontext, Decimal as D
  >>> D(1) / D(8)
  Decimal('0.125')
  >>> with localcontext(prec=2):
  ...   D(1) / D(8)
  ...
  Decimal('0.12')
  >>> with localcontext(prec=2, rounding=decimal.ROUND_CEILING):
  ...   D(1) / D(8)
  ...
  Decimal('0.13')
Laws can specify which settings to use, for examples, https://www.law.cornell.edu/cfr/text/40/1065.20 includes "Use the following rounding convention, which is consistent with ASTM E29 and NIST SP 811",

  (1) If the first (left-most) digit to be removed is less than five, remove all the appropriate digits without changing the digits that remain. For example, 3.141593 rounded to the second decimal place is 3.14.

  (2) If the first digit to be removed is greater than five, remove all the appropriate digits and increase the lowest-value remaining digit by one. For example, 3.141593 rounded to the fourth decimal place is 3.1416.

   ... (I've left out some lines)
and from https://www.law.cornell.edu/cfr/text/7/1005.83 :

  (3) Divide the result in paragraph (a)(2) of this section by 5.5, and round
  down to three decimal places to compute the fuel cost adjustment factor;

  (4) Add the result in paragraph (a)(3) of this section to $1.91;

  (5) Divide the result in paragraph (a)(4) of this section by 480;

  (6) Round the result in paragraph (a)(5) of this section down to five decimal
  places to compute the mileage rate. 
There's probably laws which require multiple and different rounding modes in the calculation.

This means simply doing all of the calculations in scaled bigints or as fractions won't really work.

Now of course, you could indeed handle all of this with prefix functions and with explicit context in the function call, but it's going to be more verbose, and obscure the calculation you want to do. I mean, it's not seriously worse. Compare:

  with localcontext(prec=3, rounding=decimal.ROUND_DOWN):
    line3 = line2 / D("5.5")
  line4 = line3 + D("1.91")
  line5 = line4 / 480
  line6 = line5.quantize(D('.00001'), rounding=decimal.ROUND_DOWN)
vs. some function-based API with overloaded parameter types:

  line3 = decimal_div(line2, D("5.5"), prec=3, rounding=decimal.ROUND_DOWN)
  line4 = decimal_add(line3, D("1.91"))
  line5 = decimal_div(line4, 480)
  line6 = decimal_quantize(line5, D('.00001'), rounding=decimal.ROUND_DOWN)
But it is worse. I also originally made a typo in the function-based API for line5 where I used "decimal_add" instead of "decimal_div" - the symbols "/" and "+" stand out more, and are less likely to be copy&pasted/auto-completed incorrectly.

If overloaded parameters - "spooky action at a distance vibes" - also aren't allowed, then this becomes more rather more complicated.

Re:C++23: The Next C++ Standard by dekhn on Tuesday July 11, 2023 @06:09PM (#36687919)
plus(x, y) IS mathematical notation. It's RPN without symbol shortcuts. Mathematica has used this for decades.
Re:C++23: The Next C++ Standard by danzk on Tuesday July 11, 2023 @06:38PM (#36688193)
It's Polish Notation actually.
Re:C++23: The Next C++ Standard by dekhn on Tuesday July 11, 2023 @06:56PM (#36688332)
Thanks, corrected in place.
Re:C++23: The Next C++ Standard by spacechild1 on Tuesday July 11, 2023 @08:00PM (#36688775)
sure, I was a bit sloppy there, but I think you know what I meant. Polish notation is just not how most people write arithmetic expressions :)
Re:C++23: The Next C++ Standard by kazinator on Tuesday July 11, 2023 @07:07PM (#36688405)
mul(x, y)

can be overloaded! x and y could be any cobination of matrix, vector, or integer, float.

You're mixing up overloading (which is semantics) with syntax.

Re:C++23: The Next C++ Standard by spacechild1 on Tuesday July 11, 2023 @11:34PM (#36690186)
We all know that functions can be overloaded. The emphasis is on operator overloading. So yes, it is about syntax.
Re:C++23: The Next C++ Standard by slavik81 on Tuesday July 11, 2023 @03:23PM (#36686013)
Why have operators at all? If that notation is good enough, then you might as well use it for the built-in types too. We're halfway to designing a Lisp!
Re:C++23: The Next C++ Standard by pjmlp on Tuesday July 11, 2023 @03:24PM (#36686037)
How can you be sure that plus() and mul() actually do what they say?
Re:C++23: The Next C++ Standard by coldpie on Tuesday July 11, 2023 @03:31PM (#36686136)
They're functions. Whatever they do, my code will go execute some code from whatever library implements them, which is what a function does. I just want to be able to rely on [] being an array subscript when I read some unfamiliar code. Is that too much to ask?
Re:C++23: The Next C++ Standard by pjmlp on Tuesday July 11, 2023 @04:28PM (#36686799)
Just like the function that happens to be named [].
Re:C++23: The Next C++ Standard by kristiandupont on Wednesday July 12, 2023 @01:14AM (#36690686)
Can IDE's detect this and offer "go to implementation" on an overloaded operator these days? Because besides from the surprise-element in the fact that there even is code to debug hiding somewhere, not being able to quickly navigate to it is much worse in my opinion. And with infix operators where you can't even be sure which operand the implementation belongs to, figuring it out can be a bit of a detective task.
Re:C++23: The Next C++ Standard by pjmlp on Wednesday July 12, 2023 @02:56AM (#36691238)
Visual Studio does show a popup, go to implementation, and if the implementation happens to be visible on the same screen, also highlights it.
Re:C++23: The Next C++ Standard by circuit10 on Tuesday July 11, 2023 @07:23PM (#36688506)
If you’re using it right then [] is still an array subscript, just with a custom implementation for getting array elements
Re:C++23: The Next C++ Standard by throwaway894345 on Tuesday July 11, 2023 @03:32PM (#36686152)
You read the code. And unlike operator overloading, you know at a glance exactly which implementation to look at. There is no spooky action at a distance.
Re:C++23: The Next C++ Standard by joshuamorton on Tuesday July 11, 2023 @04:05PM (#36686539)
How, the functions might support dynamic dispatch or be overloaded based on types.

There's nothing that prevents me from implementing all of

    plus(Int, Int)
    plus(Int, Vec)
    plus(Int, Mat)
    plus(Vec, Vec)
    plus(Vec, Mat)
    plus(Mat, Mat)
and to know which `plus` is being dispatched, you need to know the types of both arguments, exactly the same as if `plus` is named `__add__` in python or `operator+` in C++.
Re:C++23: The Next C++ Standard by throwaway894345 on Wednesday July 12, 2023 @08:09AM (#36693362)
Yes, overloading is bad (operator overloading is a kind of overloading).
Re:C++23: The Next C++ Standard by joshuamorton on Wednesday July 12, 2023 @04:44PM (#36701398)
How do you do math without overloading? I have ints, floats, bignums, vectors, matrices, complex numbers, and potentially fractions.

You want every operation to have a distinctly named version for each other numeric type?

Maybe you can reduce some of those, but you can't really have interfaces without some overloading.

Re:C++23: The Next C++ Standard by pjmlp on Tuesday July 11, 2023 @04:27PM (#36686784)
Exactly the same thing with functions that happen to use funny characters.
Re:C++23: The Next C++ Standard by throwaway894345 on Wednesday July 12, 2023 @08:09AM (#36693356)
Nah, there’s no spooky action at a distance when you don’t overload. You know concretely what code is running.
Re:C++23: The Next C++ Standard by pjmlp on Wednesday July 12, 2023 @10:11AM (#36694827)
Have you ever heard of linkers?
Re:C++23: The Next C++ Standard by throwaway894345 on Wednesday July 12, 2023 @01:30PM (#36698320)
Have you ever heard of potatoes? Neither of them help you tell at a glance which code is being dispatched to.
Re:C++23: The Next C++ Standard by Etherlord87 on Tuesday July 11, 2023 @03:51PM (#36686400)
I think in this context what's important is if these functions are overloaded or not…
Re:C++23: The Next C++ Standard by pjmlp on Tuesday July 11, 2023 @04:30PM (#36686823)
Which in any case also means the only way to be sure is to track down the implementation, the actual character set hardly makes a difference.
Re:C++23: The Next C++ Standard by enriquto on Tuesday July 11, 2023 @04:51PM (#36687051)
I agree. Function overloading is just as bad as operator overloading.
Re:C++23: The Next C++ Standard by pjmlp on Wednesday July 12, 2023 @02:33AM (#36691087)
You still need to validate if the implementation matches the name, no matter what.
Re:C++23: The Next C++ Standard by ThemalSpan on Tuesday July 11, 2023 @02:46PM (#36685510)
Sorry but please don't take Eigen (https://eigen.tuxfamily.org) away from me. Can't speak for others, but the scientific code I work on would become unreadable like that.
Re:C++23: The Next C++ Standard by sva_ on Tuesday July 11, 2023 @03:33PM (#36686158)
Re:C++23: The Next C++ Standard by bigbillheck on Tuesday July 11, 2023 @03:31PM (#36686123)
Function overloading's not that much better.
Re:C++23: The Next C++ Standard by dleslie on Tuesday July 11, 2023 @04:53PM (#36687080)
Even better:

    (plus 
      (mul vec1 vec2)
      (mul vec3 vec4))
Lisp is love.
Re:C++23: The Next C++ Standard by Koshkin on Tuesday July 11, 2023 @05:56PM (#36687782)
Love is cruel; you could fall for a goat (a Russian proverb)
Re:C++23: The Next C++ Standard by pjmlp on Wednesday July 12, 2023 @03:58AM (#36691614)
I rather type,

    (+
       (* vec1 vec2)
       (* vec3 vec4))
Lisp is love indeed.
Re:C++23: The Next C++ Standard by fsloth on Tuesday July 11, 2023 @02:45PM (#36685489)
That't literally the only place where operator overloading makes any sense.

In other places they monkey patch c++ defincies as a language.

And they are confusing and error prone.

Nobody is pretending we will get rid of any c++ syntax ever. So the discussion is about a hypothetical language syntax that fits C++ slot.

In that world C++ would have N x M matrices as native value types in the language (as fortran does) and those operators would be defined in the language spec for matrix types just as they are defined for standard number types at the moment.

And it would not have any operator overloading.

Re:C++23: The Next C++ Standard by doodpants on Tuesday July 11, 2023 @03:09PM (#36685849)
> That't literally the only place where operator overloading makes any sense.

That may be true for C++ (I'll take your word for it), but not for all programming languages in general. For example, in C# it's fairly common to overload == and != to implement value equality for reference types (classes).

Of course, you should really only do this for immutable classes that are mostly just records of plain old data. And C# 9 introduced record classes, which is a more convenient way of defining such classes. But record classes still overload these operators themselves, so you don't have to do it manually.

Re:C++23: The Next C++ Standard by throwaway894345 on Tuesday July 11, 2023 @03:35PM (#36686195)
Honestly that sort of thing always confused me when I worked in Java, C#, etc. I could never tell at a glance whether the operator was doing an identity comparison or a value comparison, and I definitely contributed a few bugs from this misunderstanding. In Go which lacks operator overloading, we either `ptr1 == ptr2` or we do some `ptr1.Equals(ptr2)` for value comparisons and `ptr1 == ptr2` for pointer comparisons--in either case, there is no ambiguity and IME fewer bugs.
Re:C++23: The Next C++ Standard by wizofaus on Tuesday July 11, 2023 @04:28PM (#36686798)
Java's the same regarding == and .equals( ) and when it's Java code written by devs who also work in other languages, it definitely still results in bugs, sometimes that go undiscovered for remarkably long times (particularly if == happens to return the right result in most cases). Meaning/needing to compare references for string (and similar) types is exceedingly uncommon, yet uses the more "natural" syntax for testing equality. FWIW I can't remember working with a codebase where unexpected behavior due to operator overloading was a serious problem.
Re:C++23: The Next C++ Standard by gpderetta on Tuesday July 11, 2023 @05:50PM (#36687722)
That's more of a problem with languages that confuse value types and references.
Re:C++23: The Next C++ Standard by crickey on Tuesday July 11, 2023 @02:55PM (#36685642)
Making such assumptions about what is correct or proper use is why c++ is so successful, it doesnt make assumptions it leaves it up to the project / community using it. Go ahead make a language that dictates alot and makes srict assumptions it will be depricated or forced to open up before the end of the decade. Notr this is why i think python and lisp is so populare meta programing is very powerful and expressive.
Re:C++23: The Next C++ Standard by suby on Tuesday July 11, 2023 @06:07PM (#36687901)
That's a good observation about C++ not making assumptions, it strikes me as true. C++ apparently doesn't even make assumptions about what the C++ filename extension is. .h, .hh, .hpp, .hxx, .C, .cc, .cpp, .cxx, .ixx, cppm
Re:C++23: The Next C++ Standard by fsloth on Wednesday July 12, 2023 @09:34AM (#36694290)
The fact C++ has so many ways of defining things is not the reason it's so popular. The reason is the enormous industry investment on the language tooling and ecosystem. IMO the language itself is the worst part of the ecosystem, but the other parts create a totality that is the best development language ecosystem in industry for my niche (graphics and geometry) including libraries, copmpilers, debugger & profiling etc.

Any language with the level of industrial support C++ has had would have grown to prominence. C++ came abut a judicious time in history when "object orientation" was becoming the latest buzzword. And now we have ended up with gazillions of lines of C++ code.

It's a tragedy of our trade that two mongrels - C++ and Javascript - became to be among the most prominent in our trade.

Re:C++23: The Next C++ Standard by crickey on Wednesday July 12, 2023 @12:35PM (#36697241)
But the reason C++ fits in so many industries from embedded system to high level gui libraries is its flexibility we see the end of OOP trend but C++ does bot lock its users into one paradigm or another so it will continue to be industry standard. Even if the industry is moving towards other paradigms of programing.
Re:C++23: The Next C++ Standard by masfuerte on Tuesday July 11, 2023 @03:00PM (#36685717)
I recently wrote a program to generate tilings. It used exact coordinates internally represented by algebraic numbers with integer coefficients. If I couldn't have overloaded the arithmetic operators then using the algebraic numbers would have been a massive pain in the arse. Overloading is legitimately useful.
Re:C++23: The Next C++ Standard by noduerme on Tuesday July 11, 2023 @03:50PM (#36686384)
What about the assignment operator?
Re:C++23: The Next C++ Standard by eesmith on Tuesday July 11, 2023 @05:55PM (#36687780)
> That't literally the only place where operator overloading makes any sense.

So, no overloading for decimal types? Nor fraction types?

I may be the oddball here, but I've used operator overloading on those types more than I have for vectors and arrays.

Re:C++23: The Next C++ Standard by kazinator on Tuesday July 11, 2023 @07:11PM (#36688431)
Fortran now has operator overloading, though.
Re:C++23: The Next C++ Standard by kristiandupont on Tuesday July 11, 2023 @02:49PM (#36685548)
Vectors and perhaps matrices are about the only valid use case I have ever come across, so I agree with GP that it's not worth it. And that's speaking as someone who once implemented a path class with a subtraction operator that would return the relative path between two absolute ones. I thought I was very clever. I feel sorry for the developers who had to figure out what that was about after I left..
Re:C++23: The Next C++ Standard by coldpie on Tuesday July 11, 2023 @03:01PM (#36685728)
Ah, matrices! Does * mean dot product, cross product, or the usually less useful matrix multiplication? Ooh, or maybe you should you use . for dot! How clever that would be!

> And that's speaking as someone who once implemented a path class with a subtraction operator that would return the relative path between two absolute ones. I thought I was very clever.

Haha! It's ok. The temptation to be clever with operators is too strong, few can resist before getting burned (or more usually, burning others!) at least once.

Re:C++23: The Next C++ Standard by owalt on Tuesday July 11, 2023 @05:03PM (#36687185)
> Ah, matrices! Does * mean dot product, cross product, or the usually less useful matrix multiplication? Ooh, or maybe you should you use . for dot! How clever that would be!

Why the snark? The fact that you're free to make a bad choice does not imply that having a free choice must be bad. Obviously neither dot nor cross product should be *. It should be the Hadamard product or matrix multiplication. You can choose one convention for your code and be perfectly happy for it.

As a follow-up question: How do you feel about languages like Fortran and Matlab then? Is it actually a good thing that mathematics convenience features are relegated to a few math-oriented languages and kept away from all the others? (Or are the linear algebra types in these languages offensive as well?)

Re:C++23: The Next C++ Standard by kps on Tuesday July 11, 2023 @06:40PM (#36688216)
Maybe C++26 will let us write operator·() and operator×().
Re:C++23: The Next C++ Standard by enriquto on Tuesday July 11, 2023 @07:15PM (#36688451)
In C++34 we'll finally have a way of overloading the empty string operator, so that we can, at last, write AB for the product of matrices A and B. As God intended.
Re:C++23: The Next C++ Standard by andrewf on Tuesday July 11, 2023 @08:25PM (#36688942)
Stroustrup, B. (1998, April 1) Generalizing Overloading for C++2000. AT&T Labs. https://www.stroustrup.com/whitespace98.pdf
Re:C++23: The Next C++ Standard by woooooo on Tuesday July 11, 2023 @06:45PM (#36688248)
The benefits from operator overloading are "I can show this to a mathematician and it looks like what they're used to". The downsides lurk in the corners of whether it's actually doing what you think.

I'll pass.

Re:C++23: The Next C++ Standard by bhouston on Tuesday July 11, 2023 @03:43PM (#36686275)
As someone who has written math libraries over and over again for the last 25 years (I wish I was joking, but it turns out it is something I'm good at [1]), I find that operator overloading works only for the simple cases but that for performance and clarify, function names work best.

Function names let you clarify that it is an outside product or inside product (e.g. there are often different types of adds, multiplies, divides), and I can not stand when someone maps cross product onto ^ (because you can both exponent and cross product some vectors, like quaternions, so why use exponent operator for cross?) or dot product onto something else that doesn't make any sense. Also operator overloading often doesn't make clear memory management, rather it relies on making new objects constantly, whereas with explicit functions, you can pass in an additional parameter that will take the result. Lastly, explicit functions allow you to pass additional information like how to handle various conditions, like non-invertible, divide by zeros, etc.

I find word based function more verbose but significant less error prone and also they are more performant (because of the full control over new object creation.) Operator overloading is only good for very simple code and even then people always push it too far so that I cannot understand it.

[1] 1997: https://github.com/bhouston/BezierCurveDemo1997/blob/master/..., 2001: C# math library: https://github.com/bhouston/ExoEngine3D/tree/master/Librarie..., 2013: a bunch of the core Three.js math library, 2023: https://github.com/bhouston/threeify/tree/master/packages/ma...

Re:C++23: The Next C++ Standard by spacechild1 on Tuesday July 11, 2023 @04:00PM (#36686497)
> rather it relies on making new objects constantly, whereas with explicit functions, you can pass in an additional parameter that will take the result.

We have move semantics since C++11.

Re:C++23: The Next C++ Standard by gpderetta on Tuesday July 11, 2023 @05:34PM (#36687524)
And expression templates even before that.
Re:C++23: The Next C++ Standard by dryanau on Wednesday July 12, 2023 @12:29AM (#36690485)
It's not the same if you need to allocate memory for the result. If you could pass the result in by reference, then you could (re)use a buffer which has already been allocated. The difference is massive in things like matrix calculations or image processing where you have an inner loop or a real-time stream repeating similar calculations.
Re:C++23: The Next C++ Standard by bhouston on Wednesday July 12, 2023 @06:31AM (#36692562)
Or you are working with a language like JavaScript where math primitives are GC objects and thus quite costly. In those languages if you do not reduce object creation via reuse in this way, it can be very slow.
Re:C++23: The Next C++ Standard by anon291 on Tuesday July 11, 2023 @10:44PM (#36689876)
Perhaps you're arguing that you ought to be able to name new operators (like Haskell) so that you can create a new operator for inner product instead of having to use '^' (typically used for exp or xor).

Alternatively, the main reason to use operators here is infix notation, so perhaps Haskell-like backticks.

Re:C++23: The Next C++ Standard by gmadsen on Tuesday July 11, 2023 @10:47PM (#36689899)
I think languages like Julia make a strong case the other way. you can literally write algorithms that match the pseudocode in a paper. You have to be ok with unicode in your source file, but for numeric stuff, I think its a nice feature
Re:C++23: The Next C++ Standard by Etherlord87 on Tuesday July 11, 2023 @03:48PM (#36686356)
What if you could type the asterisk to multiply vectors, but then your editor of choice would replace it with the symbol that actually means vector multiplication?

Perhaps that idea falls apart once you realize you would need hundreds of symbols for just addition…

But what if those symbols were (automatically) imported and named at the beginning?

Perhaps it would be annoyingly inconsistent how in various files different symbols are used for the same operation…

Re:C++23: The Next C++ Standard by JohnFen on Tuesday July 11, 2023 @04:40PM (#36686918)
> then your editor of choice would replace it

I realize this is a minority opinion, but I don't want my editor to replace anything unless I've deliberately told it to.

Re:C++23: The Next C++ Standard by gpderetta on Tuesday July 11, 2023 @05:51PM (#36687735)
It could annotate the view without changing the source.

For example my editor annotates auto with inferred types and function parameters with parameter names.

Re:C++23: The Next C++ Standard by Etherlord87 on Wednesday July 12, 2023 @05:06AM (#36692033)
The idea is, operator overloading is a convenience feature. Why not have that convenience as an option in an editor, without influencing the language? If you want scalar multiplication to look the same as vector multiplication, set it in your editor. If you want to insert scalar multiplication with the same key you insert vector multiplication, set it in the editor (to figure out which you mean, based on context, when you press that key).

Just to be clear, I'm not being a smartass, just considering this as an option and wondering if the HN crowd has some thoughts on this.

Re:C++23: The Next C++ Standard by JohnFen on Tuesday July 11, 2023 @04:51PM (#36687042)
Sure, there is a place for operator overloading.

That said, in my experience over the decades, operator overloading has been one of the primary causes of bugs that are very hard to pin down, so I have come to hate it. It hides far too much.

The cost/benefit ratio of operator overloading is generally unfavorable in practice, in my experience. Which is not to say it shouldn't be used when it actually clarifies things! But those situations tend to be fairly niche.

Interestingly, where I work right now, using operator overloading is specifically prohibited. So I'm confident that my dislike of the practice is not just a personal quirk.

Re:C++23: The Next C++ Standard by Q6T46nT668w6i3m on Tuesday July 11, 2023 @05:07PM (#36687229)
Yes (I’m a numerical analysis researcher and wrote a handful of ubiquitous mathematical packages)! The implementation of even primitive types can vary considerably! It’s way too much to hide. Nevertheless, I understand I’m biased. :)
Re:C++23: The Next C++ Standard by nunuvit on Wednesday July 12, 2023 @10:51AM (#36695419)
In engineering practice, we often start using math without first consulting numerical analysts. It takes a long time to identify and fix the inevitable issues, which eventually becomes a lesson we have to teach students and practicing engineers because there's so much history of doing it the wrong way.

As an example, early device models for circuit simulation were not designed to be numerically differentiable, leading to serious numerical artifacts and performance issues. Now we have courses dedicated to designing such models, and numerical analysis is used and emphasized throughout.

Is there anything today that you look at and think "yeah, they're gonna need to fix that at some point"?

Re:C++23: The Next C++ Standard by duped on Tuesday July 11, 2023 @05:30PM (#36687476)
No but I think

    sum({ mul(vec1, vec2), mul(vec3, vec4) })
definitely is.
Re:C++23: The Next C++ Standard by dekhn on Tuesday July 11, 2023 @06:07PM (#36687895)
In a certain pedantic, functional way, yes. For fast visual parsing, no.
Re:C++23: The Next C++ Standard by kazinator on Tuesday July 11, 2023 @07:06PM (#36688401)
Overloading is orthogonal to the issue you're striking at: infix operators versus postfix function calls. Functions can be overloaded just like operators.
Re:C++23: The Next C++ Standard by achates on Wednesday July 12, 2023 @08:48PM (#36703694)
I'd prefer MultiplyVecs(v1, v2) + MultiplyVecs(v3, v4) myself. Makes it easy to jump to the function definition and see exactly what's going on.