What are good use-cases for using tuples in C++11? For example, I have a function that defines a local struct as follows:
template<typename T, typename CmpF, typename LessF>
void mwquicksort(T *pT, int nitem, const int M, CmpF cmp, LessF less)
{
struct SI
{
int l, r, w;
SI() {}
SI(int _l, int _r, int _w) : l(_l), r(_r), w(_w) {}
} stack[40];
// etc
I was considering to replace the SI
struct with an std::tuple<int,int,int>
, which is a far shorter declaration with convenient constructors and operators already predefined, but with the following disadvantages:
- Tuple elements are hidden in obscure, implementation-defined structs. Even though Visual studio interprets and shows their contents nicely, I still can't put conditional breakpoints that depend on value of tuple elements.
- Accessing individual tuple fields (
get<0>(some_tuple)
) is far more verbose than accessing struct elements (s.l
). - Accessing fields by name is far more informative (and shorter!) than by numeric index.
The last two points are somewhat addressed by the tie
function. Given these disadvantages, what would be a good use-case for tuples?
UPDATE Turns out that VS2010 SP1 debugger cannot show the contents of the following array std::tuple<int, int, int> stack[40]
, but it works fine when it's coded with a struct. So the decision is basically a no-brainer: if you'll ever have to inspect its values, use a struct [esp. important with debuggers like GDB].
T*, size
? lolwot, why would you ever do such a thing. – Marquet&vec[0]
. – Avignon&vec[0]
." So pass astd::vector<T>&
rather than aT*
and make the API more sensible. (BTW, being aggressive towards the people you expect to help you is more than slightly stupid.) – Whoopstd::vector
would prevent the function from working with plain arrays. 2) The question was about use-cases for tuples in C++, not soliciting comments about coding style. – Avignonv.size()
is nicer from the end-user perspective than&v[v.size()]
, and the latter will most probably trigger a debug assert. And length isint
as unsigned sizes used in STL interfaces are a pile of crap. – Avignonv.begin()
andv.end()
, orstd::begin(arr)
andstd::end(arr)
. Done. – Shaquitashara