4 ms·
I'm super interested how you can do this in C++. Say, I need aggregate struct with a few 16 and 32 bit fields, some are little endian and some big endian. I do
by 112233 1y ago
I'm super interested how you can do this in C++. Say, I need aggregate struct with a few 16 and 32 bit fields, some are little endian and some big endian. I do not want C++ to let me mix up endianness. How do I do it?
- platinumrad 1y agoC: 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.
- NekkoDroid 1y agoIn 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.
- bluGill 1y agoThough 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.
- pjmlp 1y agoThankfully some folks already thought that out, one possible library, https://mpusz.github.io/mp-units/latest/ https://mpusz.github.io/mp-units/latest/
- bluGill 1y agoI 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
- NekkoDroid 1y agoI 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.
- bluGill 1y agoNow 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
- binary132 1y agowho said `operator*` needs to return the same type as its parameters?
- bluGill 1y agooperator* 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.
- graealex 1y agoThese 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)
- graealex 1y agoAs 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.
- rpnx 1y agoGC 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.
- bluGill 1y agoThere 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.
- pebal 1y agoThe C++ standard has never included a garbage collector. It only provided mechanisms intended to facilitate the implementation of a GC, but they were useless.
- bluGill 1y agoI 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.
- secondcoming 1y agoC++ really needs something like `using explicit kilometer = uint32_t;` The 'explicit' would force you to use static_cast
- graealex 1y agoThere's several libraries, including some supporting units and mathematical operations yielding the correct result types. And as usual, it mostly comes with zero overhead, beyond optional runtime range checking and unit conversions. But C++ is a meta-programming language. Making up your own types with full operator overloading and implicit and explicit conversions is rather easy. And the ADA feature of automatically selecting a suitable type under the hood isn't actually that useful, since computers don't really handle that many basic types on a hardware level. (And just to be clear, C++ templates can do the same either way)
- 112233 1y agoBut do these libraries allow using values in aggregates (i.e. structs that can be initialized by listing members in {} )? While preventing endianness errors