Variadic templates

前端 未结 8 1934
野趣味
野趣味 2020-12-06 00:05

I have seen a lot of links introducing the variadic templates. But I have never seen any compilable example that demonstrates this approach.

Could someone provide me

8条回答
  •  半阙折子戏
    2020-12-06 00:48

    A very simple example of variadic template:

    Suppose we want to have a function which takes variable number of arguments and prints them all. For ex:

    print("Hello", 1, 3.14, 5L);
    

    For that functionality to work, we would basically require two functions:

    First one, a function which takes variable number of arguments:

    template
    void print(T t, Args ...args){
         std::cout << t << ", ";
         print(args...);
    }
    

    Some explanation:

    1.) Parameter Packs denoted by ellipsis(...), that appear in parameter list.

    typename...Args 
            |  | << Optional whitespace. Can have multiple whitespaces in between them
        Args...args
    

    That means, these all are same.

    typename ...args
    typename...args
    typename   ...   args
    

    So, you don't have to worry about the correct position of the whitespace in there. Though, IMO at most one whitespace should be used as a best practice.

    2.) Pack Expansion: A pattern followed by an ellipsis.

    print(args...); //expand when you wish to use them
    

    3.) Parameter pack accepts zero or more template args. So, print(T t, Args... args) accepts one or more args.


    Once you understand that, we can visualize the call flow as below:

    print("Hello", 1, 3.14, 5L);
    

    translates into:

    print(string, int, float, long);
    

    which calls

    print(int, float, long);
    

    which calls

    print(float, long);  // say Level 2
    

    which calls

    print(long);         // say Level 1
    

    which calls

    print();             // say Level 0
    

    If you have followed the Point#3 carefully, you must have realized that print(T t, Args... args) can't handle call at Level 0.
    So we need another function here with same name to catch up at any level >=0.


    Second one, a function to grab the call at the top of call stack:

    Catch at level 0:

    void print(){}
    

    or, Catch at level 1:

    template
    void print(T t){ std::cout << t;}
    

    or, Catch at level 2:

    template
    void print(T t, U u){ std::cout << t << ", " << u;}
    

    so on...

    Any of these would work. Hope this helps you next time you go about writing such function or class.

提交回复
热议问题