Site icon Learn C++

How To Use Tuple Addressing Via Type In C++14 And Beyond

How To Use Tuple Addressing Via Type In C++14 And Beyond

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.

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++,

[crayon-664c4c1fb1a68128500716/]

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,

[crayon-664c4c1fb1a6e925745642/]

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,

[crayon-664c4c1fb1a70092509530/]
[crayon-664c4c1fb1a72523614100/]
[crayon-664c4c1fb1a74775066987/]
[crayon-664c4c1fb1a75695408809/]

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.

[crayon-664c4c1fb1a77587970951/]

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,

[crayon-664c4c1fb1a78854991067/]

Here is the output.

[crayon-664c4c1fb1a7a627840171/]

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.

Exit mobile version