Tuples and user-defined structs/classes each have their strengths:
Advantages of tuples:
std::get
, std::tuple_cat
, std::make_tuple
Advantages of structs/classes:
In general, tuples are great for small, temporary groupings of values, especially when returning data from functions. They allow bundling data without defining a dedicated type.
However, when a collection of data has a clear identity and will be used in many places, defining a struct or class is usually better. It makes the code more readable and allows encapsulating related functionality with the data.
As a rule of thumb, if you find yourself using std::get<3>
, std::get<8>
, etc. with a tuple, consider defining a struct with named fields instead. The resulting code will be clearer and more maintainable.
Answers to questions are automatically generated and may not have been reviewed.
std::tuple
A guide to tuples and the std::tuple
container, allowing us to store objects of different types.