Site icon Learn C++

How To Use std::apply With Tuple In C++ 17 And Beyond?

How To Use stdapply With Tuple In C++ 17 And Beyond

The C++17 standard was one of the major standards in the history of modern C++. One of the new library features in C++17 was std::apply which is designed to be used with std::tuple class template. In this post, we explain how to use apply with std::tuple in C++ examples.

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. In another term tuple stores the different types of the elements, it also supports empty lists. This template is available since C++11 and improved in C++20 standards.

std::tuple is a generalization of std::pair. The destructor of tuple is trivial if std::is_trivially_destructible<type>::value is set to true for every type in Types.

Here is an example to define different types of members in a tuple, and printing out them,

[crayon-673f2b740b919022362542/]

and the output is,

[crayon-673f2b740b922156253865/]

If you want to know more about std::tuple please check our posts below,

What is std::apply in C++?

The std::apply (also called Tuple Apply) is a template defined in <tuple> header that invokes a callable with arguments extracted from a given pair or tuple.

Syntax of std::apply since C++17 until C++23,

[crayon-673f2b740b924573031333/]

Syntax of std::apply since C++23,

[crayon-673f2b740b92b745849403/]

How can we use std::apply with a function in C++?

We can use std::apply with functions. In this example we have a function that calculates area of a ring. We use std::make_pair() to define r1 and r2 pair.

[crayon-673f2b740b92d821928919/]

Output will be as below,

[crayon-673f2b740b92f850396431/]

How can we use std::apply with a lambda definition in C++?

We can use std::apply with lambda definitions too. We use std::make_pair() to define r1 and r2 pair. Here is an example,

[crayon-673f2b740b931139267912/]

Output will be as below,

[crayon-673f2b740b933022925528/]

How can we use std::apply with tuple in C++ 17?

Now let’s use both function and lambda examples above with a tuple. Our tuple will be formed with two floating numbers that can be used as r2 and r1 parameters.

[crayon-673f2b740b937311479101/]

Is there a full example about std::apply in C++ 17 and beyond?

Here is a full example of std::apply that is used with a function and lambda in the tuple of modern C++,

[crayon-673f2b740b939133767952/]

Here is the output of full example about tuple apply,

[crayon-673f2b740b93b651727287/]

For more details about this feature in C++17 standard, please see these papers; P0220R1

C++ Builder is the easiest and fastest C and C++ compiler and IDE for building simple or professional applications on the Windows operating system. 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 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