4 ms·
Can you create a named struct on the fly? Don't you have to define struct before? return (firstPlayer:localPlayer, secondPlayer:connectedPlayer). You'd have t
by seivan 11y ago
Can you create a named struct on the fly? Don't you have to define struct before?
return (firstPlayer:localPlayer, secondPlayer:connectedPlayer).
You'd have to create the struct before you can use it?
Tuples allows usage a couple of times before you'd define a struct/class.
- temac 11y agoWhy would you want to create a named tuple by invoking convoluted code (and arguable with the end result being also not trivial to use) instead of just declaring a structure? This article is interesting to explore the technical capabilities of modern C++, but not to use the result...
- bpicolo 11y agoLightweight, low-overhead to use, iterable, self-documenting object
- temac 11y agoGiven the implementation, there is nothing lightweight and low-overhead to use at build time, and at runtime how exactly is this lighter than a struct? As for self-documenting, I don't even know what it would mean here. Remains "iterable". Given how modern compiler works, I'm still convinced you should convert plain tuples back and to a struct for source code simplicity and build time speed -- because it won't even do extra copies in most cases (and even if it does, who cares?)
- seivan 11y agoBecause I don't know what I want until I want it. I do a similar Tuple a couple of times I know I am going to need a container class/struct to avoid rewriting the same Tuple over and over again and it would also make things clearer as I know got a class/struct for that particular use case. Now until I know I need one, it's unnecessary to clutter the namespace with yet another container class/struct. Named Tuple has a few benefits, that's all. Until they don't.
- mgraczyk 11y agoYou can do this in C++ 14. auto f() -> decltype(auto) { ... return (struct { int firstPlayer; int secondPlayer; }) {localPlayer, connectedPlayer}; }