The constructor for a tuple to unpack to a class

Source: Internet
Author: User
Tags unpack

First of all, our first step is, of course, to unpack the tuple. Tuple provides a get function to get the nth element. For example:get<1> (Make_tuple (...)); To disassemble a tuple completely, it can be parsed by invoking the function multiple times, for example: Auto Tup = Make_tuple (...); Func (get<0> (tup),get<1> (tup),get<2> (tup) ......get<n> (Tup)); in fact, 0,1,... n This sequence we can construct a std::index_squence<0,1,2,..., n&gt, where n = std::tuple_size< decltype (tup) >::value- 1, then we pass template<size_t ... i,typename t> auto func (std::index_squence<i...>, t&& t) {  Return funcc_ (get<i> (t) ...);} In this way, all the elements of a tuple can be unpacked. One of the std::index_squence<0,1,2,..., n> is obtained by std::make_index_sequence<n+1>. Now unpack the constructor for the class, if you have the following scenario: Auto Tup = Make_tuple (...); A (tup); we just need to define a few of the following constructors. struct a{//The first called constructor, where a index_squence is generated and the second constructor is called. Template<typename t> A (T T): A (std::make_index_squence< std::tuple_size<t>::value >,t) {}// Here is the second constructor, which will unpack the tuple and then call the third constructor template<size_t ... i,typename t> a (Std::index_squence<i...>,t t ): A (get<i> (t) ...) {}//The third constructor, which is the direct use of a normalThe constructor for the parameter. A (...) {}}

A tuple is unpacked to the constructor of a class

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.