Print spaces between each element using a fold expression

后端 未结 4 1242
猫巷女王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:55

    You can reuse print() to achieve this behaviour. Afterall you are doing a fold operation which is by definition resursive.

    Live Demo

    template
    
    struct List
    {
        static void print_()
        {
         std::cout<::print();
    
        }
    };
    

    If you want to process many elements this way you might run into problems with template depth (gcc for instance has a limit of 900). Lucky for you you can use the -ftemplate-depth= option to tweak this behaviour.

    You can compile with -ftemplate-depth=100000 and make it work. Note that compilation time will skyrocket (most likely) or in thhe worst case you run out of memory.

提交回复
热议问题