Print spaces between each element using a fold expression

后端 未结 4 1241
猫巷女王i
猫巷女王i 2021-01-21 05:20

I am using a fold expression to print elements in a variadic pack, but how do I get a space in between each element?

Currently the output is \"1 234\", the desired outpu

4条回答
  •  被撕碎了的回忆
    2021-01-21 05:41

    In general, you use recursion for tasks like this.

    You have to define what happens when there are 2 or more and 1 elements in the list and recursively fall back to those definitions:

    template  struct List;
    template  struct List {
        static void print() {
            std::cout << First << " ";
            List::print();
        }
    };
    template  struct List {
        static void print() {
            std::cout << Last;
        }
    };
    

提交回复
热议问题