存储任意长度(非常量)的std::数组集

Storing set of std::arrays of any (but constant) length

本文关键字:std 数组 常量 任意长 非常 存储      更新时间:2023-10-16

是否有一种方法可以存储任何长度(但常量)的std::数组,其中数组的长度可以稍后在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。我只知道我有一组在编译时定义的常量数组,并且应该可以在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;
}

假设您想调用somefunc,同时使用const&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();
  }
}

生活例子

我猜你想要的是像dynarray这样奄奄一息的东西。

你当然不能在常量表达式中使用它的大小,因为它不是编译时常量(这就是名称中的"dyn"所指的)。

要在常量表达式中使用container_size,类型A必须是文字类型,所以它必须有constexpr构造函数,如果你想传递vector,这将是非常困难的!

你可以这样做,但这很恶心:

#include <array>
struct A
{
    void* ap;
    std::size_t n;
    template<std::size_t N>
      constexpr A(std::array<int, N>& a) : ap(&a), n(N) { }
    template<std::size_t N>
        std::array<int, N>& get() const { return *static_cast<std::array<int, N>*>(ap); }
    constexpr std::size_t size() const { return n; }
};
int main()
{
    static std::array<int, 3> a{ 1, 2, 3 };
    constexpr A aa(a);
    constexpr int len = aa.size(); // OK, this is a constant expression
    std::array<int, aa.size()>& r = aa.get<aa.size()>();        
}