6 ms·
Named tuple for C++
- cbsmith 11y agoYup. I reached the same idea a while ago. I didn't use a hash for the fields though. If you think about it, you don't need a hash function at all. A hash function actually slows down compile times! At compile time, you just resolve all the names to offsets in the tuple (simpler, and the added benefit of no hash collisions). I created a "Symbol" type for string constexpr's and used a user defined literal for them. It seems to me like Boost.Hana is the natural way to do it.
- jbandela1 11y agoLooks very interesting, do you have any examples online?
- daemin 11y agoSo you effectively make an enum out of the names in the tuple. That way you can even use the enum to use as the index into the get<> function calls.
- cbsmith 11y agoYes, that is one way of looking at it, though enums don't use string constants.
- gnuvince 11y agoI don't know C++, so sorry if this is a dumb question: what's the difference between a tuple with named fields and a struct?
- plorkyeran 11y agoYou can do metaprogramming things like iterate over the fields of a tuple or unpack it into the arguments of a function, but you can't with a struct (without manually defining an array of fields).
- melted 11y agoYou use this if you want to _appear_ smart, and you use structs if you actually _are_ smart. Smart programmers do not introduce gratuitous complexity into their code.
- temac 11y agoWTF? Just use a struct. And interop with existing tuples; maybe conversion constructor and operator cast. 10 lines everybody can understand, not some crazy shit.
- tines 11y agoI think the difference is that you can't (afaik) generate a struct with metaprogramming.
- jlarocco 11y agoBut you can generate them with macros. The Boost preprocessor library can help a lot. 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.
- cbsmith 11y agoYes, lots of people do named tuples with preprocessor logic of one kind or another (protoc and a variety of serialization libraries basically give this to you). It's much cleaner if you can use the type system to do the job for you.
- marshray 11y agoIt's been a while since I even considered doing something like this, but you could probably generate them by recursively inheriting from a chain of generated base classes. That said, most everyone who's looked at the problem has ended up going with macros instead. :-)
- tines 11y agoI'm not sure you could do what this does and give the members of the struct a user-supplied name though.
- bobmoretti 11y agoThe std::tie function can also solve this problem in most situations, if you are stuck with C++ 11.
- cbsmith 11y agoNot really. You don't get the names of your fields from std::tie. std::tie is more of a way to map non-named tuples to and from other data structures.
- jbandela1 11y agoLooks very nice. The nice thing about tuples is they can be manipulated generically to do some really cool stuff like destructuring and pattern matching, serialization, remote marshalling, etc. Making them easier to use is a win for all.
- makecheck 11y agoJust use two parallel containers: one for names, and one for values. It is dead simple to check that they have the same size and iterate over both simultaneously where needed. And the best part is, for code that does not actually require both pieces of information, you can pass one of the lists and pretend that the other does not exist. Code that only knows how to visit values only has to deal with values and not weird hybrid name/value elements. 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.
- daemin 11y agoOr you know, actually make a class that contains the data and a constructor. Or even a simple struct if one is allergic to classes for simple types. For the example given in the article a simple class or struct is already a better and easier data structure to use and store.
- nhatbui 11y agoIs this only appropriate for the case where we want to iterate over all the elements? Don't we lose the O(1) access using a name? I'm assuming you're making 2 arrays.
- makecheck 11y agoYes. My point is to consider what is actually required before making complex structures. It is often perfectly acceptable to combine two existing types and handle what few interactions they actually have.
- deleted 11y ago[deleted]
- chris_wot 11y agoWhy does this require C++14?
- allending 11y agoFrom article: > 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.
- bnastic 11y agoI always shy away from clever solutions that involve semantics that's not obvious to the reader. If something using this code lends on your lap you are going to spend hours tracking its source, figuring out what the author was trying to say, etc. 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.