C++C++17C++20Learn 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,

and the output is,

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,

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

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.

Output will be as below,

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,

Output will be as below,

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.

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

Here is the output of full example about tuple apply,

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

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

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.

Oh hi there 👋
It’s nice to meet you.

Sign up to receive awesome C++ content in your inbox, every day.

We don’t spam! Read our privacy policy for more info.

About author

Dr. Yilmaz Yoru has 35+ years of coding with more than 30+ programming languages, mostly C++ on Windows, Android, Mac-OS, iOS, Linux, and some other operating systems. He graduated and received his MSc and PhD degrees from the Department of Mechanical Engineering of Eskisehir Osmangazi University. He is the founder and CEO of ESENJA LLC Company. His interests are Programming, Thermodynamics, Fluid Mechanics, Artificial Intelligence, 2D & 3D Designs, and high-end innovations.
Related posts
C++C++11C++14C++17C++20Introduction to C++Learn C++

Learn Copy Constructors in C++ Classes

C++C++11C++14C++17Introduction to C++Learn C++Syntax

Learn How To Use Types Of Destructors In C++?

C++C++11C++14Learn C++Syntax

How To Convert u32string To A wstring In C++

C++C++11C++14C++17C++20Introduction to C++Learn C++

How To Learn The Move Constructors In Modern C++?