C ++ 17倍表达语法

时间:2018-07-19 13:47:09

标签: c++ c++17 fold-expression

我在第18行和第23行中使折叠表达式的代码正常工作

我想要取得这样的结果

"1 2 3 4"
"9 0 -1 -200"
"abc"
" world"
"Empty List"

如果列表为空,您将打印"Empty List",如果不是,则类型为char将不会打印空间,如果类型不是char,则会打印空间在它们之间打印空间。

我尝试使用((std::cout<<" " << list), ...);,但是它将打印多余的空间,因此我将其存储在临时字符串中,然后将其擦除。

有人可以帮忙吗?

#include <iostream>
#include <string>
template<int ... intlist>
using IntList = typename Facility<int>::List<intlist...>;

template<char ... charlist>
using CharList = typename Facility<char>::List<charlist...>;

template<short ... shortlist>
using ShortList = typename Facility<short>::List<shortlist...>;

template<unsigned short ... shortlist>
using UnsignedShortList = typename Facility<unsigned short>::List<shortlist...>;

template<long ... list>
using LongList = typename Facility<long>::List<list...>;

template<typename T , typename Comp=std::less<T>>
struct Facility
{
  template<T ... list> 
  struct List
  {
    static void print()
    {
      std::string str; 
      str ="\"";
      if(sizeof...(list)== 0)
      {
         str+="Empty List";
      }
      else if (std::is_same<T, char>::value)
      {
        str+=(... + list);
         //((std::cout<< list), ...);
      }
      else
      {
        str+=((" " + list), ...);
        //((std::cout<<" " << list), ...);
        str.erase(0,1);
      }
      str+="\"";
      std::cout << str << std::endl;
    }  
  }; 
};

int main()
{
    using List1 = IntList<1,2,3,4>;
    using List2 = IntList<9, 0, -1, -200>;
    List1::print();
    List2::print();

    using String1 = CharList<'a', 'b', 'c'>;
    using String2 = CharList<' ', 'w', 'o', 'r', 'l', 'd' >;
    using EmptyString = CharList<>;
    String1::print();
    String2::print();
    EmptyString::print();

  }

2 个答案:

答案 0 :(得分:1)

据我了解,您可能会使用:

template<typename T>
struct Facility
{
    template <T ... list>
    struct List
    {
        static void print()
        {
            std::cout << '"';
            if constexpr (sizeof...(list) == 0)
            {
                 std::cout << "Empty List";
            }
            else if constexpr (std::is_same<T, char>::value)
            {
                ((std::cout << list), ...);
            }
            else
            {
                [[maybe_unused]]  const char* sep = "";
                (((std::cout << sep << list), sep = " "), ...);
            }
            std::cout << '"' << std::endl;
        }  
    }; 
};

使用方式:

int main() {
    Facility<int>::List<>::print();
    Facility<int>::List<42, 42>::print();
    Facility<char>::List<'h', 'e', 'l', 'l', 'o'>::print();
}

Demo

答案 1 :(得分:0)

另一种解决方案是使用std::ostringstream并删除最后一个字符(将空格放置在最后位置)

    std::ostringstream oss;

    ((oss << list << ' '), ...);

    str += oss.str().substr(0, oss.str().size()-1);

以下是完整的编译示例

#include <string>
#include <iostream>
#include <sstream>

template<typename T , typename Comp=std::less<T>>
struct Facility
{
  template<T ... list> 
  struct List
  {
    static void print()
    {
      std::string str;

      str = "\"";

      if(sizeof...(list)== 0)
      {
         str += "Empty List";
      }
      else if (std::is_same<T, char>::value)
      {
        std::ostringstream oss;

        ((oss << list), ...);

        str += oss.str();
      }
      else
      {
        std::ostringstream oss;

        ((oss << list << ' '), ...);

        str += oss.str().substr(0, oss.str().size()-1);
      }

      str += "\"";

      std::cout << str << std::endl;
    }  
  }; 
};

int main ()
{
  Facility<int>::List<1, 2, 3, 4> f;

  f.print();
}