Comments on: Named tuple for C++ http://vitiy.info/named-tuple-for-cplusplus/ Programming, architecture and design (С++, QT, .Net/WPF, Android, iOS, NoSQL, distributed systems, mobile development, image processing, etc...) Mon, 11 Sep 2017 22:56:00 +0000 hourly 1 https://wordpress.org/?v=5.4.2 By: fish2000 http://vitiy.info/named-tuple-for-cplusplus/#comment-45974 Mon, 11 Sep 2017 22:56:00 +0000 http://vitiy.info/?p=638#comment-45974 I loved this idea – but I found it hard to use beyond the toy example the blog entry furnishes. This was because if you try to return a named tuple, you have to have a consistent return type; I used some simple macros to declare a named tuple type to use in this way:

#define SLOT_TYPE(__str__, __type__) param(__str__) = std::remove_reference_t{}

#define DECLARE_NAMED_TUPLE(…) decltype(namedtup::make_named_tuple(__VA_ARGS__))

… right? but then if you are using a tuple in a return statement like:

return make_named_tuple(param(“yodogg”) = 42);

… that yields a different type than, say:

int integer = 42;
return make_named_tuple(param(“yodogg”) = integer);

… the named parameter in first example, with the literal integer, has type int whereas the second example (in which the param value is stored in a variable) has type int &.

This makes the named tuple idiom, as implemented here, much less useful. I have tried to monkey around with this code, in order to deal with references in the manner I’ve described here, but it seems non-trivial to implement (especially in the case of struct types, where pass-by-reference is desirable). If there’s a solution to this, I’d be interested in seeing what it is, as I still like the named tuple idea. Yes!

]]>
By: Victor Laskin http://vitiy.info/named-tuple-for-cplusplus/#comment-45955 Mon, 14 Mar 2016 10:08:00 +0000 http://vitiy.info/?p=638#comment-45955 Manu, Thanks a lot for info. I guess the fix is simple – just enter right prime number. Please write me when the fix will be merged inside your ctti lib.

]]>
By: Manu343726 http://vitiy.info/named-tuple-for-cplusplus/#comment-45954 Sun, 13 Mar 2016 14:49:00 +0000 http://vitiy.info/?p=638#comment-45954 Hi Victor, I forgot to tell you that Jonathan Müller noticed a couple of months ago the factor being used for the constexpr hash is not prime. I have an open issue in our ctti library regarding this https://github.com/Manu343726/ctti/issues/11

]]>
By: Victor Laskin http://vitiy.info/named-tuple-for-cplusplus/#comment-45945 Tue, 05 Jan 2016 11:49:00 +0000 http://vitiy.info/?p=638#comment-45945 This is solution when you don’t want to add some new structure/enum into global space. The best alternative in that case would be using anonymous structs from C++11. And they are good enough for a lot of such cases.

Named tuple has advantages of std::tuple. As they have comparison operation out of the box you can pass result into std::sort for example. And more – such structure could be extended to support additional functionality in future (such as serialisation to JSON, easy introspection, concatenation of such tuples, iterations over list of members, etc)

]]>
By: Milan Jarić http://vitiy.info/named-tuple-for-cplusplus/#comment-45944 Mon, 04 Jan 2016 18:04:00 +0000 http://vitiy.info/?p=638#comment-45944 And why is this preferred comparing to defining struct type if nameing is important? It is less readable this way and I see in comments enum which I would rather use than making so much annotations and reusable for other tuples with same or similar structure

]]>
By: Victor Laskin http://vitiy.info/named-tuple-for-cplusplus/#comment-45943 Sun, 03 Jan 2016 20:47:00 +0000 http://vitiy.info/?p=638#comment-45943 You have to put enumeration into global scope. Can’t really use something like this:

auto f = [](){
enum { GPA, grade, name };
auto student = make_tuple(3.8, ‘A’, “Lisa Simpson”);
return student;
};

And if you go this way – it’s better use custom struct instead of enum.

Once again – proposed solution is for cases where you used std::pair / std::tuple in your code. I’m not suggesting to replace structs/classes as return types.

]]>
By: Keith A. Lewis http://vitiy.info/named-tuple-for-cplusplus/#comment-45942 Sun, 03 Jan 2016 18:10:00 +0000 http://vitiy.info/?p=638#comment-45942 How is this better than:
enum { GPA, grade, name };
auto student = make_tuple(3.8, ‘A’, “Lisa Simpson”);
cout << "GPA: " << get(student)
<< "grade: << get(student)
<< "name" << get(student) << endl;
?

]]>
By: Named tuple for C++ | Daily Hackers News http://vitiy.info/named-tuple-for-cplusplus/#comment-45941 Sun, 03 Jan 2016 02:21:37 +0000 http://vitiy.info/?p=638#comment-45941 […] Source link […]

]]>