The tuple ( std::tuple
) is a class template that stores the different types of elements, it also supports empty lists in modern C++. This template was introduced in C++11 and improved in C++14. In this post, we explain tuple addressing via type features that come with the C++14 standard.
Table of Contents
What is std::tuple in C++?
The tuple ( std::tuple
) is a class template a fixed-size collection of different types of values like floats, integers, texts, etc. and it is defined in the tuple header. In another term tuple stores the different types of elements, it also supports empty lists. This template has been available since C++11 and improved in C++14, and C++20 standards.
In C++ programming, the std::tuple
is a generalization of std::pair
. The destructor of the tuple is trivial if std::is_trivially_destructible::value
is set to true for every type in Types.
What is the syntax for std::tuple in C++?
Here is the syntax of std::tuple
in C++,
1 2 3 |
template< class... Types > class tuple; |
Is there a C++ std::tuple example, how can we use tuples?
Here is a example to define different types of members in a my_tuple,
1 2 3 4 5 6 7 |
std::tuple my_tuple { "text one", 9, 2.22, "text two", 15 , 7.88, "text three"}; std::cout << std::get<0>(mytuple) << std::endl; std::cout << std::get<1>(mytuple) << std::endl; std::cout << std::get<2>(mytuple) << std::endl; |
How to use tuple addressing via type feature in C++14 and beyond?
The std::tuple
type introduced in C++11 and improved in C++14 extends with the tuple addressing by type feature that allows fetching from a tuple by type instead of by index.
Here are the new std::get templates defined in the <tuple> header of C++14 and beyond,
1 2 3 4 |
template< class T, class... Types > constexpr T& get( tuple<Types...>& t ) noexcept; |
1 2 3 4 |
template< class T, class... Types > constexpr T&& get( tuple<Types...>&& t ) noexcept; |
1 2 3 4 |
template< class T, class... Types > constexpr const T& get( const tuple<Types...>& t ) noexcept; |
1 2 3 4 |
template< class T, class... Types > constexpr const T&& get( const tuple<Types...>&& t ) noexcept; |
Is there a simple tuple addressing via type example in C++14 and beyond?
Here is a simple example that shows we can get the value of an int type from tuple.
1 2 3 4 5 |
std::tuple< std::string, int, float> mytuple( "LearnCPlusPlus.org", 9, 2.45 ); int x = std::get<int>(mytuple); // get value of int type, x = 9 |
Is there a full example about the tuple addressing by type in C++14 and beyond?
Here is a full example about the tuple addressing by type in C++14 and beyond,
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 |
#include <iostream> #include <tuple> #include <string> int main() { std::tuple< std::string, int, float> mytuple( "LearnCPlusPlus.org", 9, 2.45 ); // C++11 feature, std::get value by index double d = std::get<2>(mytuple); // get value by index std::cout << d << std::endl << std::endl; std::cout << std::get<0>(mytuple) << std::endl; std::cout << std::get<1>(mytuple) << std::endl; std::cout << std::get<2>(mytuple) << std::endl << std::endl; // C++14 feature, std::get value by type std::string s = std::get<std::string>(mytuple); // get value of string type int x = std::get<int>(mytuple); // get value of int type float f= std::get<float>(mytuple); // get value of float type std::cout << s << std::endl; std::cout << x << std::endl; std::cout << f << std::endl << std::endl << std::endl; // C++17 feature, const auto[...] const auto [ title, value, score] = mytuple; std::cout << title << std::endl; std::cout << value << std::endl; std::cout << score << std::endl; system("pause"); return 0; } |
Here is the output.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
2.45 LearnCPlusPlus.org 9 2.45 LearnCPlusPlus.org 9 2.45 LearnCPlusPlus.org 9 2.45 Press any key to continue . . . |
Note that, if the tuple has more than one same element of the type, a compile-time error results.
C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows, MacOS, iOS & Android operating systems. It is also easy for beginners to learn with its wide range of samples, tutorials, help files, and LSP support for code. RAD Studio’s C++ Builder version comes with the award-winning VCL framework for high-performance native Windows apps and the powerful FireMonkey (FMX) framework for cross-platform UIs.
There is a free C++ Builder Community Edition for students, beginners, and startups; it can be downloaded from here. For professional developers, there are Professional, Architect, or Enterprise versions of C++ Builder and there is a trial version you can download from here.