相关疑难解决方法(0)

如何在编译时生成嵌套循环

我有一个整数N,我在编译时知道.我还有一个std :: array,它包含描述N维数组形状的整数.我想在编译时使用元编程技术生成嵌套循环,如下所述.

constexpr int N {4};
constexpr std::array<int, N> shape {{1,3,5,2}};


auto f = [/* accept object which uses coords */] (auto... coords) { 
     // do sth with coords
}; 

// This is what I want to generate.
for(int i = 0; i < shape[0]; i++) {
     for(int j = 0; j < shape[1]; j++) {
          for(int k = 0; k < shape[2]; k++) {
                for(int l = 0; l < shape[3]; l++) …
Run Code Online (Sandbox Code Playgroud)

c++ templates metaprogramming c++11 c++14

28
推荐指数
1
解决办法
2290
查看次数

标签 统计

c++ ×1

c++11 ×1

c++14 ×1

metaprogramming ×1

templates ×1