Print spaces between each element using a fold expression

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

    you can that

    #include 
    
    template
    struct Facility
    {
    template
    struct List
    {
        static void print()
        {
         std::cout<<"\"" << head;
         ((std::cout << " " << list), ...);
          std::cout<<"\"";
        }
    };
    };
    
    template
    using IntList = typename Facility::List;
    int main()
    {
     using List1 = IntList<1,2,3,4>;
     List1::print();
    }
    

    the fold expression ((std::cout << " " << list), ...) will expands to ((std::cout << " " << list1), (std::cout << " " << list2), (std::cout << " " << list3)...)

提交回复
热议问题