3 ms·
I've never found a use for std::tuple. It comes with a lot of inconvenience without providing much power. Boost Fusion on the other hand is extremely powerful
by nly 11y ago
I've never found a use for std::tuple. It comes with a lot of inconvenience without providing much power.
Boost Fusion on the other hand is extremely powerful. It's relatively straightforward, for example, to write a function that takes a std::function<R(A,B,C) and returns a new function<R(vector<string>) that performs argument count checking and lexical interpretation before invoking it. Doing that with std::tuple would be a mission.
- jzwinck 11y agoTuples are useful for writing fairly low level features such as marshalling between threads. std::bind is another way, if you need to marshal arguments with a function...but if you don't have a function to call, it's just a tuple.
- eloff 11y agoLambda captures are great for that too, especially since C++14, where you can use move semantics with the captures (e.g. marshal std::unique_ptr)
- green7ea 11y agoI've found tuples to be very useful as a temporary structure. I find a tuple is to a structure what a lambda is to a function. The most useful time I've used a tuple was to build OpenGL vertex buffer objects in a more dynamic manner. You can find the code here (https://github.com/green7ea/OpenGL-Triangle/blob/master/src/triangle.cpp https://github.com/green7ea/OpenGL-Triangle/blob/master/src/...) from line 62 to 70. The big win is that most if not all of the work is done at compile time and there is no space overhead to any of it. Using boost fusion here would have been overkill as it would have added a lot of extra dependencies. I get that it's more powerful but for a lot of low level work, the extra power isn't necessary and a boost dependency isn't required.
- kryptiskt 11y agoIt works well as a key in maps when you aren't really interested in having a data structure containing those things. Also, returning a tuple and destructuring it with tie is pretty convenient.