简体   繁体   English

在编译时将std :: array的每个元素相乘

[英]Multiplying each element of an std::array at compile time

I would like to convert an std::array to another std::array , multiplying each of its elements by a specific number. 我想将std::array转换为另一个std::array ,将每个元素乘以一个特定的数字。

What I have right now obviously doesn't work: 我现在拥有的东西显然不起作用:

#include <array>
#include <iostream>
#include <utility>

template <class T, size_t... Is, size_t N>
constexpr std::array<T, N> multiply(std::array<T, N> const &src,
                                  std::index_sequence<Is...>) {
    return std::array<T, N>{{src[Is]...}}; // How can I multiply each of src's elements?
}

int main(int argc, char *argv[]) {
    constexpr std::array<int, 3> arr = {1, 2, 3};
    constexpr auto t = multiply(arr, std::make_index_sequence<3>{});
    for (auto &el : t) std::cout << el << std::endl;
    return 0;
}

My question is: how can I iterate over each element at compile time or how can I apply the same function (in my case: multiply by 2) at compile time? 我的问题是:如何在编译时迭代每个元素,或者如何在编译时应用相同的函数(在我的情况下:乘以2)?

You can do it in the following way: 您可以通过以下方式执行此操作:

template<typename T>
constexpr T mult(T const &a, T const &b) { return a * b; }

template <class T, size_t... Is, size_t N>
constexpr std::array<T, N> multiply(std::array<T, N> const &src, 
                                    std::index_sequence<Is...>) {
  return std::array<T, N>{{mult(src[Is], src[Is])...}}; 
}

Live Demo 现场演示

Or if you want to multiply by a number you can change to: 或者,如果您想乘以一个数字,您可以更改为:

template<typename T>
constexpr T mult(T const &a, T const &b) { return a * b; }

template <class T, size_t... Is, size_t N>
constexpr std::array<T, N> multiply(std::array<T, N> const &src, 
                                    std::index_sequence<Is...>, T const &mul) {
  return std::array<T, N>{{mult(src[Is], mul)...}}; 
}

Live Demo 现场演示

As Explained in cppreference : 正如cppreference中所解释的那样:

A pattern followed by an ellipsis, in which the name of at least one parameter pack appears at least once, is expanded into zero or more comma-separated instantiations of the pattern, where the name of the parameter pack is replaced by each of the types from the pack, in order. 后跟省略号的模式,其中至少一个参数包的名称至少出现一次,将扩展为模式的零个或多个以逗号分隔的实例化,其中参数包的名称由每个类型替换从包装,按顺序。 Pack expansions can only happen in pack expansion contexts. 包扩展只能在包扩展上下文中发生。 These essentially are : 这些基本上是:

  • braced initialization 支持初始化
  • initializer lists 初始化列表
  • aggregate initializations 聚合初始化
  • function calls 函数调用
  • array initializations 数组初始化

Edit: 编辑:

As TC pointed in the comments you can also do it as simple as: 正如TC在评论中指出的那样,你也可以这么简单:

template <class T, size_t... Is, size_t N>
constexpr std::array<T, N> multiply(std::array<T, N> const &src, std::index_sequence<Is...>, T const &mul) {
  return std::array<T, N>{{(src[Is] * mul)...}}; 
}

Live Demo 现场演示

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM