One of my colleagues has asked me, "why not use a simple struct over a tuple?" Unfortunately, I couldn't give a convincing answer, which means I probably don't understand tuple well enough. What is a good way to respond to this? Then we can identify when it makes better sense to use a struct versus a tuple. Thanks, Graham
On 9/5/07, Graham Reitz
One of my colleagues has asked me, "why not use a simple struct over a tuple?"
Unfortunately, I couldn't give a convincing answer, which means I probably don't understand tuple well enough.
What is a good way to respond to this? Then we can identify when it makes better sense to use a struct versus a tuple.
Arguably a tuple is simpler (to use) than a struct :) Richard
Graham Reitz wrote:
One of my colleagues has asked me, "why not use a simple struct over a tuple?"
func( make_tuple(1,'2',"foo") ); func( make_tuple("foo", "bar") ); Note that there is no way to define a struct within an expression. Also note that 'func' might be a single function template that can work with any tuple. You can't deal in such generic way with a struct because you never know what names the members have. Just try to write a template that takes an arbitrary struct and attempts to sum up all its (data) members with operator+ (that is, without cheating by providing extra-information) - it's impossible.
Unfortunately, I couldn't give a convincing answer, which means I probably don't understand tuple well enough.
Think of Tuple as a heterogeneously-typed array. We can also have maps (indexed by type) and operations (that return an altered Tuple). That's what the Fusion library (available in the development version of Boost) is all about.
What is a good way to respond to this? Then we can identify when it makes better sense to use a struct versus a tuple.
Using a Tuple as a drop-in replacement for some struct is probably just bad style, as we usually want our code to be self-descriptive and to compile as fast as possible. A Tuple is appropriate when we'd otherwise have to establish weird conventions for some template to work with a struct (e.g. calling its members 'm1', 'm2', 'm3', ... and adding some static constant for the number of members ;-) ). Regards, Tobias
On 9/4/07, Graham Reitz
One of my colleagues has asked me, "why not use a simple struct over a tuple?"
Unfortunately, I couldn't give a convincing answer, which means I probably don't understand tuple well enough.
What is a good way to respond to this? Then we can identify when it makes better sense to use a struct versus a tuple.
It probably depends on the application, but tuples are nice for
returning multiple values from a function. For example
boost::tuple
Graham Reitz wrote:
One of my colleagues has asked me, "why not use a simple struct over a tuple?"
Unfortunately, I couldn't give a convincing answer, which means I probably don't understand tuple well enough.
What is a good way to respond to this? Then we can identify when it makes better sense to use a struct versus a tuple.
In a word, "genericity". Yeah, there's no such word in the english dictionary, but I'm sure you know what I mean. When you want to treat something in a uniform manner, then you'll want to use tuples. Tuple genericity is exploited by the Boost.Fusion library such that you have iterators and algorithms that work on different tuple types (think STL for heterogeneous types). And it goes further! In Fusion a struct is a tuple. See: http://tinyurl.com/2ypbof Regards, -- Joel de Guzman http://www.boost-consulting.com http://spirit.sf.net
participants (5)
-
Chris Weed
-
Graham Reitz
-
Joel de Guzman
-
Richard Dingwall
-
Tobias Schwinger