First, what is the tuple
Tuples are not new, there are tuples in math, Python language, and C + + that we're going to talk about today.
Simply put, tuples are a group of things, for example, when speaking algebraic topology, topological space X and one of the points x as a pair (x, x), which is actually a tuple, point coordinates can also be regarded as a tuple. The tuple (tuple) in C + + looks like this:
Std::tuple<int, std::string> tu{2, "12iop"};
A tuple can contain different types of members, such as the above tu containing an int and a string.
Second, usage
Before we examine the source code, we must first know its usage.
To use a tuple, include the header file <tuple>:
#include <tuple>
A tuple actually has a class template with a mutable parameter, which, when used, is passed into a number of parameters to be special.
struct point{int x; int y;}; void Main () {std::tuple<int, std::string> t1{1, "Qwer"};//a tuple consisting of int and string constexpr std::tuple<int, V Oid*> t2{1,nullptr}; A tuple std::tuple<int consisting of int and void, point> t3{1,{20,89}}; A tuple std::tuple<int consisting of an int and a point struct, char, std::string> t4{1, ' t ', ' qwer '}; A tuple that consists of int, char, string}
In the above code, I decorated the T2 with constexpr, which is perfectly correct, and the Std::tuple constructor is constexpr.
Gets the value in the tuple, using Std::get. This is not a function, but a function template, we need to pass in a variable of type size_t, or pass in a type that tells it what type of member we need to take out of the tuple.
struct point{ int x; int y;}; Void main () { std::tuple<int, std::string> t1{ 1, "Qwer" &NBSP;} ; constexpr std::tuple<int, void*> t2{ 10,nullptr }; std::tuple<int, Point> t3{ 1,{20,89} }; std::tuple<int, char, std::string> t4{ 1, ' t ', ' qwer ' }; std::cout << std::get<0> (t1) << std::endl; Constexpr int n2 = std::get<0> (T2); std::cout << n2 << std::endl; auto s = std::get<char> (T4) ; std::cout << s << std::endl;}
Std::get is also constexpr, so N2 is also a compile-time constant.
We got s in the way of Get<char>, which is a variable of type char. Std::get<t> can be obtained from a tuple to a member of the first type T.
A tuple can also compare equality with "= =" and "! =":
Std::tuple<int, std::string> t5{1, "Qwer"};if (t1 = = T5) {std::cout << "= =" << Std::endl;}
The usage of tuple is not the main content of this article, so it ends here. Interested students can check the information on their own.
Next, it's time to look at the source code.
Third, source code analysis
A tuple is a class template for mutable parameters:
Template<typename. _types>class tuple;
This is the declaration of the class template.
Next, implement an empty tuple with a zero number of parameters.
Struct allocator_arg_t{};template<>class tuple<>{public: typedef tuple<> _myt; constexpr tuple () noexcept {} template<typename _alloc> tuple (allocator_ arg_t, const _alloc&) noexcept {} constexpr tuple (const tuple&) noexcept {} template< Class _alloc> tuple (Allocator_arg_t, const _alloc&, const _Myt&) noexcept {} void swap (_Myt&) noexcept {} constexpr bool _equals (const _ myt&) const noexcept { return true; } constexpr bool _less (const _myt&) const noexcept { return false; }};
allocator_arg_t is an empty structure, for the time being. _myt is tuple<> himself, so it is easier to write.
Tuple<> defines empty constructors and copy constructors (empty tuple is nothing to do).
The member function swap is used to exchange content with another tuple<>, because there is nothing to swap, and the function body is, of course, empty.
_equals is used to determine whether the two tuple<> are equal, and it returns true, which is obvious (all tuple<> are a look).
_less from the function name, is to compare size, but if you encounter a type that does not overload <? Temporarily regardless of it.
With the definition of an empty tuple, a non-empty tuple can be defined.
Template<class _this,class _rest>class tuple<_this, _rest...>: Private tuple<_rest...>{//content}
The tuple private of N (>0) elements inherits a tuple of n-1 elements. Obviously this is a recursive definition that will eventually be recursive to Tuple<>, while tuple<> is already well defined.
For example, Tuple<int, Char, short> private inherit Tuple<char, Short>, and Tuple<char , short> and private inherit Tuple<short Thetuple<short> private inherits the tuple<>. Because private inheritance can implement the "Has-a" feature, you can combine different types of objects in such a way.
Simple analysis of C + + standard library tuple (tuple) source code