c++ - 存储任何(但恒定)长度的 std::arrays 集

标签 c++ templates c++14 constexpr stdarray

有没有办法存储一组任意(但恒定)长度的 std::arrays 数组的长度稍后可以在 constexpr 中使用?

我想标准容器是不可能的,但不知何故可能有一个模板解决方案。所有信息都在编译时可用,不是吗?

示例代码:

#include <iostream>
#include <string>
#include <vector>
#include <array>
#include <algorithm>

// Class storing an array of any (but constant) size
struct A {
    const std::vector<std::string> container; 
    const int container_size;
    A(const std::vector<std::string>& v, int size) : container(v), container_size(size) { }
};

int main() {
    // List of variable length const arrays and their sizes
    std::vector<A> myAList {
        A({ std::string("String1"), std::string("String2") }, 2),
        A({ std::string("String1") }, 1)
    };

    // How would I go about using the size of each array in a constexpr?
    for (auto const& a : myAList) {
    // Example constexpr:
    //  somefunc(std::make_index_sequence<a.container_size>{});

    // 2nd example converting to actual std::array
    //  std::array<std::string, a.container_size> arr;
    //  std::copy_n(std::make_move_iterator(a.begin()), a.container_size, arr.begin());
    }

    return 0;
}

更新:

我们要求提供更多详细信息,所以这里开始吧。我真的不在乎数组是如何定义的,任何有用的...使用的确切 constexpr 是示例代码中的那个,std::make_index_sequence<CONSTEXPR>{} . 我只知道我有一组在编译时定义的常量数组,并且应该可以以某种方式在 constexpr 中的其他地方引用它们的长度

哎呀,实际上我只需要存储长度就可以了:

// Class storing an array size
struct A {
    A(int size) : container_size(size) { }
    const int container_size;
};

int main() {
    // List of lengths
    std::vector<A> mySizeList { 2, 1 };

    for (auto const& a : mySizeList) {
    //  somefunc(std::make_index_sequence<a.container_size>{});
    }
    return 0;
}

最佳答案

这假设您要调用 somefuncconst&std::vectorindex_sequence , 你只想调用 somefunc .它还提供对 somefunc 的支持具有扩展签名(只需传递带有返回值和/或附加参数的第三个参数,这些参数将在 std::vector<T> const& 之后传递)。

如果传入的大小比 vector 长,则无法将 vector 的大小与传入的大小对齐将导致错误。

它还假定您知道在构造时调用的是什么函数。这自然可以推广到您想要在构造点调用的任何有限函数集。

所使用的技术称为“类型删除”或“运行时概念”。我删除了调用你的概念 some_func在构造期间使用有问题的索引序列,并将删除的操作存储在 f 中.

template<size_t N>
using size=std::integral_constant<size_t, N>;

template<
  class T, template<class...>class Operation,
  class Sig=std::result_of_t<Operation<size<0>>(std::vector<T> const&)>()
>
struct eraser;

template<class T, template<class...>class Operation, class R, class...Args>
struct eraser<T,Operation, R(Args...)> {
  std::vector<T> data;
  R(*f)(eraser const*, Args&&...);
  R operator()(Args...args)const{
    return f(this, std::forward<Args>(args)...);
  }
  template<size_t N>
  eraser( std::initializer_list<T> il, size<N> ):
    eraser( il.begin(), size<N>{} )
  {}
  template<class...Ts>
  eraser( T t0, Ts... ts ):
    eraser(
      {std::forward<T>(t0), std::forward<Ts>(ts)...},
      size<sizeof...(ts)+1>{}
    )
  {}
  template<size_t N>
  eraser( T const* ptr, size<N> ):
    data(ptr, ptr+N),
    f([](eraser const*self, Args&&...args)->R{
      return Operation<size<N>>{}(self->data, std::forward<Args>(args)...);
    })
  {}
};
template<class T, size_t ... Is>
void some_func( std::vector<T> const&, std::index_sequence<Is...>) {
  std::cout << "called! [#" << sizeof...(Is) << "]\n";
}
template<class N>
struct call_some_func {
  template<class T>
  void operator()(std::vector<T> const& v) const {
    some_func( v, std::make_index_sequence<N{}>{} );
  }
};


int main() {
  using A = eraser<std::string, call_some_func>;
  // List of variable length const arrays and their sizes
  std::vector<A> myAList {
    A({ std::string("String1"), std::string("String2") }, size<2>{}),
    A({ std::string("String1") }, size<1>{})
  };

  std::cout << "Objects constructed.  Loop about to start:\n";

  // How would I go about using the size of each array in a constexpr?
  for (auto const& a : myAList) {
    a();
  }
}

live example

关于c++ - 存储任何(但恒定)长度的 std::arrays 集,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31346313/

相关文章:

c++ - 用花括号 C++ 实例化一个对象

c++ - 有没有办法找出线程是否被阻塞?

c++ - 运行时固定大小的 std::vector?

templates - 如何查找具有默认值的可变数量 constexpr std::arrays 的 constexpr max

c++ - 哈希表键与取值复杂性

c++ - 引用基类模板成员变量的简单方法

c++ - Solaris CC 要求的 template<> 语法,但被 MSVC 和 GCC 禁止

c++ - C++模板函数,在仍允许内联的同时指定回调仿函数/lambda的参数类型?

c++ - 哈希的 unordered_map

c++ - 使用可变数量的浮点参数