Boost logo

Boost Users :

From: Tobias Schwinger (tschwinger_at_[hidden])
Date: 2007-09-04 17:35:17


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


Boost-users list run by williamkempf at hotmail.com, kalb at libertysoft.com, bjorn.karlsson at readsoft.com, gregod at cs.rpi.edu, wekempf at cox.net