3 ms·
No. This is how sum types are implemented. And from a runtime perspective it’s going to be a struct with perhaps more padding. You’ll need more details about y
by grandempire 1y ago
No. This is how sum types are implemented.
And from a runtime perspective it’s going to be a struct with perhaps more padding. You’ll need more details about your specific threat model to explain why that’s bad.
- mistrial9 1y agoa quick search says that std::variant is the modern replacement to implement your niche feature "sum types"
- grandempire 1y agoThat’s for C++. And how is std::variant implemented?
- LowLevelMahn 1y agonot using a union: https://ojdip.net/2013/10/implementing-a-variant-type-in-cpp/ https://ojdip.net/2013/10/implementing-a-variant-type-in-cpp... because the union can't be extended with variadic template types
- grandempire 1y agoSo instead it has a buffer large enough to hold all the types? That’s what union does. Still waiting to hear the security concerns.
- LegionMammal978 1y agoActually, it does use a union, in both libstdc++ [0] and libc++ [1]. (Underneath a lengthy stack of base classes, since it wouldn't be C++ if it weren't painful to match the specified semantics.) [0] https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libstdc%2B%2B-v3/include/std/variant;hb=refs/tags/releases/gcc-15.1.0#l386 https://gcc.gnu.org/git/?p=gcc.git;a=blob;f=libstdc%2B%2B-v3... [1] https://github.com/llvm/llvm-project/blob/llvmorg-20.1.3/libcxx/include/variant#L671 https://github.com/llvm/llvm-project/blob/llvmorg-20.1.3/lib...
- jlouis 1y agoNot a niche feature. Fundamental for any decent language with a type system.
- soraminazuki 1y agoWhoa, that's a core building block of programming and computer science that you're dismissing as "niche" without explanation.
- mistrial9 1y agoyes types are a core building block of programming and computer science, but not using UNION ? this casual dismissal of "criticisms of UNION" here seems superficial and un-wise to me.
- soraminazuki 1y agoSum types, not C unions. Different concepts. A sum type is a concept from type theory. Like unions, it expresses a type that can be either one of multiple types. But unlike unions, it retains information about which type it is. Properly implemented sum types are completely type safe. I can't be 100% sure what your particular "criticisms" of C unions precisely are, but assuming they all relate to type safety, they don't apply to sum types. Sum types are important because any real world project has to deal with data that's either A or B. There's nothing controversial here. In C, a union is a way to implement that. Yes, it's unsafe. But can you eliminate the use of unsafe features from C projects? No, if they deal with memory. Also, it's rich and quite frankly rude to brush off my comment as "casual dismissals," "superficial," and "unwise" when it's a direct response to this. > your niche feature "sum types" That's pure unprovoked smugness right there that contains no substance of what your criticisms actually are, let alone the reason.