Open In App

std::make_tuple() in C++

In C++, std::make_tuple() is a standard library function that constructs an object of std::tuple type using the arguments given to it. It can take any number and type of argument and create a tuple object by automatically deducing the type of the argument. It is defined inside the <utility> header file as a function template.

Syntax of make_tuple()

std::make_tuple(args...);

Parameters of make_tuple()

This function can take any number of arguments of any type.

Return Value of make_tuple()

This function returns the object of type std::tuple whose type is deduced from its argument type.

Example of make_tuple()

// C++ program to illustrate
// std::make_tuple() function in C++
#include <iostream>
#include <tuple>
using namespace std;

int main()
{
    // Tuple Declared and Initialized using make_tuple()
    auto t1 = make_tuple(1, "GeeksforGeeks", 'g');

    // Tuple Printed
    cout << "Tuple: " << get<0>(t1) << ", " << get<1>(t1)
         << ", " << get<2>(t1);

    return 0;
}

Output
Tuple: 1, GeeksforGeeks, g

Important Points

  1. The std::make_tuple() function is used to create a tuple object that holds any number of values of any valid C++ type. The values can be of the same type or of different types.
  2. This function is particularly useful when you want to return multiple values from a function without using out parameters.
  3. The std::make_tuple() function is a more convenient way to create tuples because it automatically deduces the type of the tuple elements from the arguments passed to it.
  4. The std::make_tuple() function is a template function, which means it can work with any data type as long as that data type supports the operations that are used in the function.


Article Tags :