Named tuple for C++
vitiy.info
vitiy.info
It seems to me like Boost.Hana is the natural way to do it.
At work we use kind of a hybrid approach. We have generator macros that spit out named tuple classes that use a bunch of magic and template meta-programming.
It should go without saying that the macro implementation is ugly and only a few people understand it very well, but we don't have to change them much any more, and the resulting named tuples are easy to use and generally work exactly as expected.
That said, most everyone who's looked at the problem has ended up going with macros instead. :-)
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.
This article is interesting to explore the technical capabilities of modern C++, but not to use the result...
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?)
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.
auto f() -> decltype(auto) {
...
return (struct {
int firstPlayer;
int secondPlayer;
}) {localPlayer, connectedPlayer};
}For example if you have a struct like:
struct data {
double w;
double x;
double y;
double z;
};
You can then create a struct of that type using syntax like: struct data d = {.w = 1.0, .x = 1.0, .y = 1.0, .z = 1.0};
Then you could have your functions take a struct data as a parameter, and use designated initializers to have syntax for keyword parameters.I'm not sure why this feature never made its way into a C++ standard over the years.
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2015/p010...
What if You by mistake switch ‘GPA’ and ‘grade’ inside your function? Nothing will happen to inform You that something went wrong.
I don't know if there's a specific name for it, but "what if you did X" arguments like this fail to convince me.
Just don't use it to replace structs, it doesn't make sense.
I know, I've been there. Making my coworkers' lives harder than it needed to be with clever template magic didn't help anyone. I ripped that code out soon after.
But if the code like this is well contained, and is there to solve a very specific problem that maintainers will be able to handle, then go for it.
std::tie is more of a way to map non-named tuples to and from other data structures.
Seriously, always try the simpler thing first. Do not go deeper into templates and special syntax before seeing if any other construct will do the job.
For the example given in the article a simple class or struct is already a better and easier data structure to use and store.
> A lot of you now are thinking about making custom struct with named fields, than and passing typed result as this struct. This is good solution and there are a lot of cases when this will be actually better, but … this is not so simple. But what if you know that this temporary structure is rather limited in size and will be used just ‘here’ to pass results...
I'd still use a custom struct with named fields. I don't see the above being anywhere close to justifying the additional complexity.