Tuples and std::tuple

Tuples vs Variadic Templates

When should I use tuples versus variadic template parameters?

Vector art representing computer hardware

Tuples and variadic templates are both ways to work with a variable number of values, but they have different use cases.

Use tuples when:

  • You need to store and pass around a fixed number of related values.
  • The values have different types and don't share a common base class.
  • You want to return multiple values from a function.
  • You need to work with the values individually using std::getstd::tuple_element, etc.

For example, a function that returns a player's name and score is a good candidate for a tuple:

std::tuple<std::string, int> getInfo(int id);

On the other hand, use variadic templates when:

  • You're writing a function template that needs to accept an arbitrary number of arguments.
  • The arguments have the same type, or share a common base class.
  • You want to perform an operation on all the arguments in a uniform way.

For instance, a function that computes the sum of an arbitrary number of integers is a good fit for variadic templates:

template <typename... Args>
int sum(Args... args) {
  return (... + args);
}

In this case, the sum function accepts any number of arguments (captured by Args...) and adds them together using a fold expression.

Sometimes, you can use the two techniques together. For example, you might have a variadic function template that forwards its arguments to another function as a tuple:

template <typename... Args>
void log(Args&&... args) {
  std::tuple<Args...> data(
    std::forward<Args>(args)...);
  writeToLog(data);
}

Here, log accepts any number of arguments, perfect forwards them to construct a tuple, and then passes that tuple to writeToLog.

In summary, use tuples for bundling related values, and variadic templates for writing functions that operate on an arbitrary number of arguments in a uniform way. The two techniques can also be combined when needed.

This Question is from the Lesson:

Tuples and std::tuple

A guide to tuples and the std::tuple container, allowing us to store objects of different types.

Answers to questions are automatically generated and may not have been reviewed.

This Question is from the Lesson:

Tuples and std::tuple

A guide to tuples and the std::tuple container, allowing us to store objects of different types.

A computer programmer
Part of the course:

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Free, unlimited access

This course includes:

  • 124 Lessons
  • 550+ Code Samples
  • 96% Positive Reviews
  • Regularly Updated
  • Help and FAQ
Free, Unlimited Access

Professional C++

Comprehensive course covering advanced concepts, and how to use them on large-scale projects.

Screenshot from Warhammer: Total War
Screenshot from Tomb Raider
Screenshot from Jedi: Fallen Order
Contact|Privacy Policy|Terms of Use
Copyright © 2024 - All Rights Reserved