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::make_from_tuple which is a new function template that initializes a value of type T
from the elements of a given tuple. In this post, we explain how to 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,
1 2 3 4 5 6 7 |
std::tuple mycard { "12345687", "Yilmaz Yoru", 42}; std::cout << "ID:" << std::get<0>(mycard) << std::endl; std::cout << "Name:" << std::get<1>(mycard) << std::endl; std::cout << "Age:" << std::get<2>(mycard) << std::endl; |
and the output is,
1 2 3 4 5 |
ID:12345687 Name:Yilmaz Yoru Age:42 |
If you want to know more about std::tuple please check our posts below,
What is std::make_from_tuple in C++?
The make_from_tuple
(std::make_from_tuple) is a new function template defined in the <tuple>
header that initializes a value of type T
from the elements of a given tuple. It is like the apply
template applied to a constructor. It can be used with a constructor of a struct or a class.
Syntax of std::make_from_tuple since C++17 until C++23,
1 2 3 4 |
template<class _Ty, class _Tuple> constexpr _Ty make_from_tuple(_Tuple&& _Tpl); |
Syntax of std::make_from_tuple since C++23,
1 2 3 4 |
template<class _Ty, tuple-like _Tuple> constexpr _Ty make_from_tuple(_Tuple&& _Tpl); |
How to use std::make_from_tuple in C++ 17
Here is a full example that uses our defined print_class constructor with std::make_from_tuple,
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 |
#include <iostream> #include <string> #include <tuple> class print_card { public: print_card(std::string id, std::string name, int age) { std::cout << "ID:" << id << std::endl; std::cout << "Name:" << name << std::endl; std::cout << "Age:" << age << std::endl << std::endl; } }; int main() { std::tuple card = { "12345687", "Yilmaz Yoru", 42 }; std::make_from_tuple<print_card>( card ); system("pause"); return 0; } |
Here is the output ,
1 2 3 4 5 6 7 |
ID:12345687 Name:Yilmaz Yoru Age:42 Press any key to continue . . . |
For more details about this feature in C++17 standard, please see these papers; P0209R2
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.