【问题标题】:c++17 fold expression print function that can support 2,0000 element efficientc++17折叠表达式打印功能,可支持20000个元素高效
【发布时间】:2018-07-20 08:33:45
【问题描述】:

您好,如何使用像 20,000 个元素这样的大元素使其打印效率更高? 我花了很长时间编译, 我试着遵循这个 static const array of values in Variadic Template C++

但是我不能让它与折叠表达式一起工作,任何想法如何解决这个问题

#include <iostream>


template<typename T = int , typename Comp=std::less<T>>
struct Facility
{
  template<T ... list> 
  struct List
  {
    static void print()
    {
      // store in static array to compile faster
      constexpr static T args[sizeof...(list)] = {list...}; 
      // use for idiom for each except the last, but this time in reserve way
      // assgn char* space to empty char first
      const char* space = "";
      if (sizeof...(list) == 0)
      {
        // simple just call space to cout , if not warning no variable used
        std::cout <<'"' <<"Empty List" <<'"' << space  <<std::endl;
      }
      else if  (std::is_same<T, char>::value)
      {
        std::cout << '"';
       ((std::cout << space << args), ...);// compile error
        // simple just call space to cout , if not warning no variable used
        std::cout << '"' << space<< std::endl;
      }
      else
      {
        std::cout << '"';
        // fold expression bracket,L side will execute first 
        // the first round space is empty and later on become space
        (((std::cout << space << list), space = " "), ...);
        std::cout << '"' << std::endl;
      }    
    }  
  }; 




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...>;

int main()
{
    std::cout << "********Testing Basic List Capabilities*********\n";  
    using List1 = IntList<1,2,3,4>;
    using List2 = IntList<9, 0, -1, -200>;
    using List3 = IntList<LONG20000LIST>;
    List1::print();
    List2::print();
    List3::print();

    using String1 = CharList<'a', 'b', 'c'>;
    using String2 = CharList<' ', 'w', 'o', 'r', 'l', 'd' >;
    using EmptyString = CharList<>;
    String1::print();
    String2::print();
    EmptyString::print();
    std::cout << "************Finished Testing *******************\n\n";
}

【问题讨论】:

  • #define LONG20000LIST 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9, 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9 , 0, 8 ,9, 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9, 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9, 0, 1 , 2, 3, 4, 5, 6, 7, 8 ,9, 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9, 0, 1, 2, 3, 4, 5, 6 , 7, 8 ,9, 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9, 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9, 0, 1 , 2, 3, 4, 5, 6, 7, 8 ,9, 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9, 0, 1, 2, 3, 4, 5, 6 , 7, 8 ,9, 0, 1, 2, 3, 4, 5, 6, 7, 8 ,9, 0, 1, 2, 3, 4, 5, 6, 7, 8 ,20000
  • 我无法添加 20000 列表定义,超过字符限制
  • 为什么要把这20000个元素列表表示为编译时间常数?你不能把它塞进一个向量然后copy它到一个std::experimental::ostream_joiner吗?
  • stackoverflow.com/questions/46454659/… 找到了解决方案,您不能在这种情况下使用 std::experimental::ostream_joiner

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


【解决方案1】:

这是我的解决方案,谢谢大家的帮助

static void print()
{
  // store in static array to compile faster
  std::vector<T> args = {list...};
  // use for idiom for each except the last, but this time in reserve way
  // assgn char* space to empty char first
  const char* space = "";
  if (sizeof...(list) == 0)
  {
    // simple just call space to cout , if not warning no variable used
    std::cout <<'"' <<"Empty List" <<'"' << space  <<std::endl;
  }
  else if  (std::is_same<T, char>::value)
  {
    std::cout << '"';
    for (auto const& elem : args)
    {
        std::cout << space << elem; 
    }
    std::cout << '"' << space<< std::endl;
  }
  else
  {
     std::cout << '"';
     for (auto const& elem : args)
     {
        std::cout << space << elem; 
        space = " ";
     }
     std::cout << '"' << std::endl;
  }    
}  

【讨论】:

  • 我想你可以在这里使用constexpr ifs。此外,即使您已经解决了问题,但没有回答您的标题问题以使用折叠表达式。有点不确定何时投反对票。
  • 不知道如果把std::vector&lt;T&gt; args = {list...};换成T args[sizeof...(list)] = {list...};,还会编译慢吗?
  • 大 constexpr 数据或大内置数组的原因是什么?
  • 我不知道如何让它在编译时快,我在网上搜索,有人说默认模板在编译时很慢。让我们看看 c++20 中的元类做了什么
  • @R2RT 是的,我在 Microsoft 编译器上使用它,如果使用 constexpr,g++ 会以某种方式抱怨
【解决方案2】:

这就是你需要声明你的数组的方式:

constexpr static T args[] = {list...};

【讨论】:

  • 那么我如何在这部分替换 else { std::cout
  • @user3770234 改成for
猜你喜欢
  • 2020-02-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-01-25
  • 1970-01-01
  • 2016-05-17
  • 1970-01-01
相关资源
最近更新 更多