News for hackers. Stuff that matters.
Re:Should I choose Ada, SPARK, or Rust over C/C++? (2024) by platinumrad on Monday October 06, 2025 @01:36AM (#45487966)
C: struct be32_t { uint32_t _ }; struct le32_t { uint32_t _ };

C++: That, but with a billion operator overloads and conversion operators so they feel just like native integers.

Re:Should I choose Ada, SPARK, or Rust over C/C++? (2024) by NekkoDroid on Monday October 06, 2025 @04:38AM (#45489085)
In C++ you probably could even make a templated class that implements all possible operators for any type that supports it with concepts. Then you can just `using kilometer = unique_type<uint32_t, "kilometer">` without needing to create a custom type each time.
Re:Should I choose Ada, SPARK, or Rust over C/C++? (2024) by bluGill on Monday October 06, 2025 @05:58AM (#45489631)
Though if you do that km times km isn't km it is a volume - so your custom type would be wrong to have all operations. what unit km times km should be isn't clear.
Re:Should I choose Ada, SPARK, or Rust over C/C++? (2024) by pjmlp on Monday October 06, 2025 @06:45AM (#45489908)
Thankfully some folks already thought that out, one possible library,

https://mpusz.github.io/mp-units/latest/

Re:Should I choose Ada, SPARK, or Rust over C/C++? (2024) by bluGill on Monday October 06, 2025 @08:10AM (#45490502)
I have seen several versions. I wrote two different ones myself - both not in use because the real world of units turned out far more complex. the multiplication thing is one simple example of the issuses but not a complete list
Re:Should I choose Ada, SPARK, or Rust over C/C++? (2024) by NekkoDroid on Monday October 06, 2025 @10:40AM (#45491933)
I guess I should have said `unique_type<uint32_t, "meter", std::ratio<1000, 1>>` then :) Then you can do the same as std::chrono::duration does.
Re:Should I choose Ada, SPARK, or Rust over C/C++? (2024) by bluGill on Monday October 06, 2025 @01:51PM (#45494067)
Now write a book on the various tradeoffs from that decision. there is no perfect general answer, some domains have specific needs that are different. Depending on your domain that might be a good choice or it might be terrible
Re:Should I choose Ada, SPARK, or Rust over C/C++? (2024) by binary132 on Monday October 06, 2025 @02:02PM (#45494204)
who said `operator*` needs to return the same type as its parameters?
Re:Should I choose Ada, SPARK, or Rust over C/C++? (2024) by bluGill on Tuesday October 07, 2025 @09:05AM (#45502589)
operator* can return exactly one type. You can choose which, but metric offers many possible choices, and with floating point math on computers you will lose precision converting between them in some cases so you need to take care to get the right on for your users - which will not be the same for all users.
Re:Should I choose Ada, SPARK, or Rust over C/C++? (2024) by binary132 on Tuesday October 07, 2025 @04:02PM (#45508109)
One return type, for any given combination of parameter types, not to mention the possibility of templating to manipulate the return type….
Re:Should I choose Ada, SPARK, or Rust over C/C++? (2024) by bluGill on Tuesday October 07, 2025 @04:51PM (#45508699)
See, more trade offs...
Re:Should I choose Ada, SPARK, or Rust over C/C++? (2024) by binary132 on Wednesday October 08, 2025 @05:32PM (#45520880)
honestly, I’m not seeing the problem you’re seeing
Re:Should I choose Ada, SPARK, or Rust over C/C++? (2024) by graealex on Monday October 06, 2025 @02:48PM (#45494812)
These libraries already exist. God how people underestimate C++ all the time.

Of course you can use a unit type that handles conversions AND mathematical operations. Feet to meter cubed and you get m³, and the library will throw a compile error if you try to assign it to anything it doesn't work with (liters would be fine, for example)

Re:Should I choose Ada, SPARK, or Rust over C/C++? (2024) by graealex on Monday October 06, 2025 @03:16PM (#45495142)
As a more general rant - people who have maybe used 5% of the feature set of C++ come along and explain why language X is superior because it has feature Y and Z.

News flash, C++ has every conceivable feature, it's the reason why it is so unwieldy. But you can even plug in a fucking GC if you so desire. Let alone stuff like basic meta programming.

Re:Should I choose Ada, SPARK, or Rust over C/C++? (2024) by rpnx on Tuesday October 07, 2025 @12:59PM (#45505664)
GC was removed from the C++ standard in C++23 because all the compilers were like "hell no" and it was an optional feature so they could get away with not adding it. So this optional feature never actually existed and they removed it in later standards.
Re:Should I choose Ada, SPARK, or Rust over C/C++? (2024) by bluGill on Tuesday October 07, 2025 @04:51PM (#45508693)
There are ways to do GC without language support. They are harder, but have been around in various forms for decades. They have never caught on though.
Re:Should I choose Ada, SPARK, or Rust over C/C++? (2024) by pebal on Tuesday October 07, 2025 @05:52PM (#45509379)
The C++ standard has never included a garbage collector. It only provided mechanisms intended to facilitate the implementation of a GC, but they were useless.
Re:Should I choose Ada, SPARK, or Rust over C/C++? (2024) by bluGill on Tuesday October 07, 2025 @09:02AM (#45502562)
I know of about 7 different libraries, 5 of them private to my company (of which 4 are not in use). Every one takes a fundamentally different approach to the problem.

> Feet to meter cubed and you get m³, and the library will throw a compile error if you try to assign it to anything it doesn't work with (liters would be fine, for example)

Liters would not be fine if you are using standard floating point values since you lose precision moving decimal points in some cases. Maybe for your application the values are such that this doesn't matter, but without understanding your problem in depth you cannot make the generic statement.

I could write books (I won't but I could) on all the compromises and trade offs in building a unit type library.

Re:Should I choose Ada, SPARK, or Rust over C/C++? (2024) by 112233 on Thursday October 09, 2025 @05:26AM (#45525376)
Do they really? Their types really have no custom constructors and you can use designated initializers for your data? I would really much like to have been underestimating C++, could you show an example of such a library?
Re:Should I choose Ada, SPARK, or Rust over C/C++? (2024) by secondcoming on Monday October 06, 2025 @06:54AM (#45489976)
C++ really needs something like `using explicit kilometer = uint32_t;`

The 'explicit' would force you to use static_cast