In this article we will discuss what is std::tuple and how to use it.
what is std::tuple and why do we need it ?
std::tuple is a type that can bind fixed size heterogeneous values together. We need to specify the type of elements as template parameter while creating tuple object.
Creating a std::tuple object
Let’s declare a std::tuple that is a collection of an int, double and std::string i.e.
// Creating a tuple of int, double and string std::tuple<int, double, std::string> result(7, 9.8, "text");
Now, all the three types of variables are encapsulated in a single object. We can return this tuple object from a function too. So, basically it helps us to return multiple values from a function. Eventually, it helps us to avoid creating unnecessary structs.
Frequently Asked:
Header file required,
#include <tuple> // Required for std::tuple
Getting elements from a std::tuple
We can get the element hidden in tuple object using std::get function by specifying the index value as template argument.
Let’s get the first element from tuple object i.e.
CODE// Get First int value from tuple int iVal = std::get<0>(result);
Similarly get the 2nd and 3rd element from tuple object i.e.
Best Resources to Learn C++:
// Get second double value from tuple double dVal = std::get<1>(result); // Get third string value from tuple std::string strVal = std::get<2>(result);
Getting Out Of Range value from tuple
Fetching any element with index more than number of elements encapsulated by tuple will cause compile time error.
For example, trying to get 4th element from tuple will cause compile time error, because this tuple object has only 3 elements i.e.
// Get 4th int value from tuple // Will cause compile error because this tuple // has only 3 elements int iVal2 = std::get<4>(result); // Compile error
Wrong type cast while getting value from tuple
Similarly using wrong type while getting element from tuple will also cause error i.e.
// Wrong cast will force compile time error // Get first value from tuple in a string std::string strVal2 = std::get<0>(result); // Compile error
Above line will cause compile error because type of first element in tuple is int not string/
Getting value from tuple by dynamic index
Template parameter in std::get whould be compile time constant otherwise it will cause compile time error i.e.
int x = 1; // Get second double value from tuple // Compile error because x is not compile time contant double dVal2 = std::get<x>(result); // Compile error
Above line will cause compile error because x is not compile time const. Where as below lines will work fine
const int i = 1; // Get second double value from tuple double dVal3 = std::get<i>(result);
Complete example is as follows,
#include <iostream> #include <tuple> // Required for std::tuple #include <string> /* * Returning multiple values from a function by binding them in a single * tuple object. */ std::tuple<int, double, std::string> someFunction() { // Creating a tuple of int, double and string std::tuple<int, double, std::string> result(7, 9.8, "text"); // Returning tuple object return result; } int main() { // Get tuple object from a function std::tuple<int, double, std::string> result = someFunction(); // Get values from tuple // Get First int value from tuple int iVal = std::get < 0 > (result); // Get second double value from tuple double dVal = std::get < 1 > (result); // Get third string value from tuple std::string strVal = std::get < 2 > (result); // Print values std::cout << "int value = " << iVal << std::endl; std::cout << "double value = " << dVal << std::endl; std::cout << "string value = " << strVal << std::endl; // Get 4th int value from tuple // Will cause compile error because this tuple // has only 3 elements //int iVal2 = std::get<4>(result); // Compile error // Wrong cast will force compile time error // Get first value from tuple in a string //std::string strVal2 = std::get<0>(result); // Compile error int x = 1; // Get second double value from tuple // Compile error because x is not compile time contant //double dVal2 = std::get<x>(result); const int i = 1; // Get second double value from tuple double dVal3 = std::get < i > (result); return 0; }
Output:
int value = 7 double value = 9.8 string value = text
Pingback: C++11 : make_tuple Tutorial & Example – thisPointer.com
Very Great article…Previously I never understood what is a tuple in c++…But now it seems very easy and very useful construct…Thanks Man.
your tutorials are very good. Simplified and explained nicely.
I felt you can provide some tutorials on template library implementation rather than explaining how particular
works explain how to implement our own library. This gives in depth knowledge in CPP. Like how to implement our own tuple class or Vector or Stack rather than depending on library.