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 ,
|