4 ms·
C++ has a zero-sized type too: it's called "void". But it's not first-class, which is a weird restriction. Having first-class "void" is very useful. For exampl
by pcwalton 12y ago
C++ has a zero-sized type too: it's called "void". But it's not first-class, which is a weird restriction.
Having first-class "void" is very useful. For example, you can make efficient hash sets out of hash maps by simply using () as the value.
- userbinator 12y agoHow would that be useful? If you need a hash set, use a hash set.
- pcwalton 12y agoFor the same reason any generic type is useful: you can use one implementation for multiple purposes (as a hash map implementation doubles as a perfectly good hash set implementation) without having to duplicate code.
- userbinator 12y agoI think that's backwards: a hash set implementation can be used as a hash map simply by storing objects whose comparison is limited to their key fields. A hash map with values that are all the same and never used just seems like a waste.
- pcwalton 12y agoIt's not a waste, because the fact that unit is zero-sized makes it boil away to nothing at runtime.