C++14 using Std::integer_sequence to expand a tuple as a function parameter

Source: Internet
Author: User
Tags variadic

A tuple is a set of fixed lengths that allow different types of elements, depending on the number of elements and respectively called a unary group, two-tuple, ternary group, and so On. The standard library in c++11 adds a class template called std::tuple, which is used to represent tuples. The following code demonstrates the creation of a ternary group using C + +. Auto tuple = std::make_tuple (1, ' A ', "test"); std::cout << std::get<0> (tuple) << Std::endl;std::cout & lt;< std::get<1> (tuple) << std::endl;std::cout << std::get<2> (tuple) << std::endl;// Here are the features of c++14 std::cout << std::get<int> (tuple) << std::endl;std::cout << std::get<char> ( Tuple) << std::endl;std::cout << std::get<const char*> (tuple) << std::endl; Output 1atest1atest Many programming languages, such as C #, python, also have the concept of tuple. The following code demonstrates the creation of a ternary group using Python. t = (1, ' A ', "test") print (t[0]) print (t[1]) print (t[2]) output 1AtestPython A parameter that supports the expansion of a tuple to a function from a language level, In python, it is assumed that a function func and a tuple T are defined, and the following code demonstrates each element of a tuple t as an argument to the Func Function. def func (arg1, arg2, arg3):    print (arg1, arg2, arg3) t = (1, ' A ', "test") func (*t) output one A test variable parameter template (Variadic Tem Plate) C + + does not provide language or library support for Python-like features, The purpose of this article is to describe how to implement a tuple expansion as a function parameter in C + +. Assume that there is a function func and tuple tuple as follows: void func(int arg1, char arg2, const std::string& arg3); {   //...} Auto tuple = std::make_tuple (1, ' A ', "test"), and manually removing elements from a tuple as parameters should be called the Following. 1func (std::get<0> (tuple), std::get<1> (tuple), std::get<2> (tuple)); Observe the parameters of the manual adjustment, you can see that for the n-tuple, The parameter that is called when the function is a list. std::get<0> (t), std::get<1> (t), ..., std::get<n–1> (t) can then use the c++11 variable parameter template (Variadic Template) Write a function template such as applytemplate<std::size_t ... I, TypeName F, TypeName t>void apply (f f, const t& T) {  func (std::get<i> (T) ...);} One of the std::size_t in line 1th ... I called the template parameter group (Parameter Pack), line 4th of std::get<i> (t): Called a parameter group unwind (Parameter Pack Expansion). Use this function template to apply a tuple expansion as a parameter called the Func function is written like This. 1apply<0, 1, 2> (func, tuple); obviously, This is not graceful enough because the template parameters need to be written manually. For an n-tuple, the template parameter here is such a sequence. 0, 1, 2, 3, 4, ..., N-1 It is much easier to derive this sequence automatically if you are able to deduce it using template argument derivation (argument deduction). This mechanism is provided by std::integer_sequence in c++14. The standard library in std::integer_sequencec++14 adds an integer sequence that is used by the Std::integer_sequence class template to represent the compilation Period. Its declaration is as follows Template<class t, t ... Ints>class integer_sequence;The following is a description of each template parameter T The type of the integer sequence element ... IntsParameter groups for integer sequences (non-types) for ease of use, The C++14 standard library also uses the c++11 template alias (templates typedef or template Alias) attribute to declare the following additional alias templates for Use. template<std::size_t ... ints>using index_sequence = std::integer_sequence<std::size_t, ints...>;template<class t, t N>using Make_integer_sequence = std::integer_sequence<t,/* a sequence 0, 1, 2, ..., N-1 */>;template<std::size_t N>us ing make_index_sequence = make_integer_sequence<std::size_t, n>;template<class ... T>using index_sequence_for = std::make_index_sequence<sizeof ... (T) >; The following code demonstrates the use of std::integer_sequence to create a vector containing elements 0, 1, 2, 3, ..., 9. The template parameter group of line 5th is 0, 1, 2, 3, ..., 9, by the template parameter 10 of line 13th. #include <utility> #include <vector> #include <iostream>template<std::size_t ... i>std::vector<std::size_t> make_index_vector (std::index_sequence<i...>) {  return {I ...};} int main () {  Auto VEC = make_index_vector (std::make_index_sequence<10> ());  for (auto i:vec) {  &NB Sp Std::cout << i << ''; }  std::cout << std::endl;} Output 10 1 2 3 4 5 6 7 8 9 use Std::integer_sequence to implement the Apply function template for a tuple can use std::tuple_size to get the number of elements of a tuple, similar to the previous creation of a vector using Std::integer _sequance the code for implementing the Apply function template is as Follows. Template<typename F, TypeName T, std::size_t ... I>void Apply_impl (f f, const t& T, std::index_sequence<i...>) {  F (std::get<i> (T) ...); Template<typename f, TypeName t>void apply (f f, const t& t) {  apply_impl (f, t, std::make_index_sequence&lt ;std::tuple_size<t>::value> ());} At this point, you no longer need to manually write template parameters when using the Apply function Template. Apply (func, tuple); The disadvantage is that if the Func function has a return value, its return value is Ignored. For problems with return values, you can use C++11 's new function declaration syntax (new Declarator Syntax) attribute to Resolve. Template<typename F, TypeName T, std::size_t ... I>auto Apply_impl (f f, const t& T, std::index_sequence<i...>), decltype (f (std::get<i> (T) ...) {  return f (std::get<i> (t) ...);} Template<typename f, TypeName T>auto apply (f f, const t& T), decltype (apply_impl (f, T, std::make_index_sequ Ence<std::tUple_size<t>::value> ())) {  return apply_impl (f, T, std::make_index_sequence<std::tuple_size<t >::value> ());} finally, Attach the complete implementation of the Apply function template and the code that uses the Demo. #include <tuple> #include <iostream> #include <string> #include <utility>int func1 (int arg1, Char arg2, double arg3, const std::string& arg4) {  std::cout << "call func1" ("<< arg1 <<", "& lt;< arg2 << "," << arg3 << "," << arg4 << ")" << std::endl;  return 0;} int FUNC2 (int arg1, int arg2) {  std::cout << "call func2 (" << arg1 << "," << arg2 << " ) "<< std::endl;  return arg1 + arg2;} Template<typename F, TypeName T, std::size_t ... I>auto Apply_impl (f f, const t& T, std::index_sequence<i...>), decltype (f (std::get<i> (T) ...) {  return f (std::get<i> (t) ...);} Template<typename f, TypeName T>auto apply (f f, const t& T), decltype (apply_impl (f, t, StD::make_index_sequence<std::tuple_size<t>::value> ())) {  return apply_impl (f, T, std::make_index_ Sequence<std::tuple_size<t>::value> ());} int main () {  using namespace std::literals::string_literals;  auto Tuple1 = std::make_tuple (1, ' A ', 1.2, "test" s);  Auto RESULT1 = apply (func1, tuple1);  std::cout << "result1 =" << result1 << std::endl;& nbsp Auto Tuple2 = std::make_tuple (1, 2);  Auto result2 = apply (func2, tuple2);  std::cout << "result2 =" < < RESULT2 << std::endl;} Output call func1 (1, A, 1.2, test) result1 = 0call Func2 (1, 2) result2 = 3

C++14 using Std::integer_sequence to expand a tuple as a parameter to a function

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.