最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

c++ - How to create a constexpr std::experimental::mdarray? - Stack Overflow

programmeradmin2浏览0评论

How can I create a constexpr std::experimental::mdarray using the reference implementation at ?

My first approach was simply to use something like constexpr std::experimental::mdarray<size_t, std::experimental::extents<size_t, 1, 1>> test_array{}; .

After this failed, I checked the code and noticed that it apparently always uses std::vector instead of std::array if there are only static extents.

What apparently works is constexpr std::experimental::mdarray< size_t, std::experimental::extents<size_t, 1, 1>, std::experimental::layout_right, std::array<size_t, 1>> test_array{};, but there I have to calculate the size of the array myself and have to switch myself back to std::vector if there is any dynamic extent. The latter would be not that nice for me, because I use this in a templated class, where some extents may be dynamic or static.

What I came up with is:

constexpr auto statistics_number_to_extent_size(const auto statistics_number) {
  assert(statistics_number >= 0);
  return statistics_number <
                 std::numeric_limits<decltype(statistics_number)>::max()
             ? statistics_number
             : std::dynamic_extent;
}

template <typename T, typename L = std::experimental::layout_right, auto... Ns>
using ConstExprMdArray = std::experimental::mdarray<
    T,
    std::experimental::extents<std::size_t,
                               statistics_number_to_extent_size(Ns)...>,
    L,
    std::conditional_t<(... ||
                        (Ns >= std::numeric_limits<decltype(Ns)>::max())),
                       std::vector<T>, std::array<T, (... * Ns)>>>;

ConstExprMdArray<F, std::experimental::layout_right, 1,
                   1>
      test_array;

However, I would have expected that there is something more convenient per default.

发布评论

评论列表(0)

  1. 暂无评论