Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using std::tuple correctly?

Tags:

c++

c++11

tuples

I recently learned how to use std::tuple (I had never needed it before then), and was curious as to when it is the proper time to use it over a container? I only ask because std::tuple doesnt feel like a container, since it requires things such as std::get and std::make_tuple.

like image 783
DTSCode Avatar asked Sep 25 '26 06:09

DTSCode


1 Answers

std::tuples contain a fixed number of elements of varying types. You use it when you want to group together a few objects of different types. I would use it over a struct when the grouping doesn't have any particularly useful semantics. For example, a std::tuple is useful when a function needs to return multiple objects. A container, on the other hand, contains multiple elements of the same type (perhaps a varying amount).

like image 60
Joseph Mansfield Avatar answered Sep 27 '26 19:09

Joseph Mansfield