Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are good use-cases for tuples in C++11?

Tags:

c++

c++11

tuples

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].

like image 570
zvrba Avatar asked Apr 21 '12 13:04

zvrba


People also ask

What are C++ tuples used for?

Tuples in C++ A tuple is an object that can hold a number of elements. The elements can be of different data types. The elements of tuples are initialized as arguments in order in which they will be accessed.

What is std :: tuple?

(since C++11) Class template std::tuple is a fixed-size collection of heterogeneous values. It is a generalization of std::pair. If std::is_trivially_destructible<Ti>::value is true for every Ti in Types , the destructor of tuple is trivial.

Do we have tuples in C?

Tuples has a sequence of elements of different data types. It was introduced to return an instance of the Tuple<T> with no need to specify the type of each element separately.

Are C++ tuples mutable?

The tuple itself isn't mutable (i.e. it doesn't have any methods that for changing its contents). Likewise, the string is immutable because strings don't have any mutating methods. The list object does have mutating methods, so it can be changed.


2 Answers

It is an easy way to return multiple values from a function;

std::tuple<int,int> fun(); 

The result values can be used elegantly as follows:

int a; int b; std::tie(a,b)=fun(); 
like image 68
mirk Avatar answered Sep 21 '22 08:09

mirk


Well, imho, the most important part is generic code. Writing generic code that works on all kinds of structs is a lot harder than writing generics that work on tuples. For example, the std::tie function you mentioned yourself would be very nearly impossible to make for structs.

this allows you to do things like this:

  • Store function parameters for delayed execution (e.g. this question )
  • Return multiple parameters without cumbersome (un)packing with std::tie
  • Combine (not equal-typed) data sets (e.g. from parallel execution), it can be done as simply as std::tuple_cat.

The thing is, it does not stop with these uses, people can expand on this list and write generic functionality based on tuples that is much harder to do with structs. Who knows, maybe tomorrow someone finds a brilliant use for serialization purposes.

like image 33
KillianDS Avatar answered Sep 20 '22 08:09

KillianDS