Variadic Templates for C ++ 0x Study Notes

Source: Internet
Author: User
Tags variadic

By feng

The introduction of Variadic Templates removes the cumbersome template features.
Example:

123456789101112131415161718192021 #include <iostream>   double do_sum() {     return 0; }   template< typename T, typename... Args > double do_sum( T&& t, Args&& ... args ) {     return t + do_sum( args... ); }   int main() {       std::cout << do_sum( 1.0, 2.0, 3.0, 4.0 )               << std::endl;       return 0; }

Note the following two points:

  • The double do_sum () function must be in the variable-length template function double do_sum (T & t, Args &&... Args) previously declared
  • The variable-length template function must be recursively implemented using another function.

You can also see that the variable length template function declaration uses... Method

  • Template <typename... Args>
  • The double do_sum (Arg... Arg)
  • Do_sum (arg…) is used in the function body ...)

It can be roughly seen that when typename exists, it is behind typename. Otherwise, it is behind Arg, and finally behind arg.

 

If you need to know how many parameters are passed in, you can do this.

 

12345 template<typename ... Args> std::size_t how_many_args(Args ... args) {     return sizeof...(args); }

 

The variadic template is basically used here, And the content below is skipped.

 

Pay attention to this...It's a little complicated.

12345678 template<typename... T> void f(T (* ...t)(int,
Related Article

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.