3 ms·
a modern tuple would be std::vector<std::any> except std::any doesn't play nice with the rest of C++ ecosystem, such as iostream.
by FrozenVoid 6y ago
a modern tuple would be std::vector<std::any> except std::any doesn't play nice
with the rest of C++ ecosystem, such as iostream.
- quietbritishjim 6y agostd::vector and std::any both allocate - compared to (say) std::tuple<int, float> that's a ludicrously heavyweight solution.
- FrozenVoid 6y agostruct{} is even more efficient, but vector<any> allows to prototype with anything.Its a more genuine 'tuple object' than std::tuple.
- arc-in-space 6y agoHuh? That's not a tuple at all! I don't understand why you say this so I'm not even sure if I should point out the obvious differences. To wit, I've literally never used vector<any>, but tuple quite a lot(though often just because it's convenient for some metaprogramming hacks).
- FrozenVoid 6y agohttp://www.goldsborough.me/cpp/2018/05/22/00-32-43-type_erasure_for_unopinionated_interfaces_in_c++/ http://www.goldsborough.me/cpp/2018/05/22/00-32-43-type_eras... https://www.oreilly.com/content/c17-upgrades-you-should-be-using-in-your-code/ https://www.oreilly.com/content/c17-upgrades-you-should-be-u...
- arc-in-space 6y agoI hope dang forgives me my tone, and my compulsion to respond to everything. I'm getting the sense you have no idea what's being talked about here, and what tuple is actually for. Instead of saying anything at all you give me two links, one of which is a 30 minute read(are you serious?) that seems to have little to do with the topic, and the other is literally just an explanation of what std::any is(really, are you serious?). I'll try to be at least somewhat charitable, and explain where I'm coming from. Tuple is a statically-typed container, that is useful in a generic context - which, crucially, doesn't mean polymorphic at runtime! - because its members' types can be inspected at compile time, unlike a struct. This makes it useful for implementing all sorts of template magic. You can also use it like a anonymous struct, for a quick-and-dirty way to return multiple values from a function, though a struct is often better. std::any, on the other hand, is, uh, not like that at all, whether it is in a vector or not. It holds a single value that is polymorphic at runtime, by erasing its type, which has nothing to do with tuples, and helps with none of the above.