小编use*_*370的帖子

C++ 张量的编译时索引/元组访问

我有一个编译时张量类。现在我想像这样实现索引访问:

std::array<std::array<std::array<int, 3>, 2>, 1> myTensor;

template <class... Indices>
auto get(Indices... indices) {
    return myTensor[indices][...];
}

int main() {
    myTensor[0][1][2] = 3;
    std::cout << get(0, 1, 2) << std::endl;
}
Run Code Online (Sandbox Code Playgroud)

但遗憾的是这不能编译。有谁知道我如何实现这个?当我的张量为常量时它也需要工作。我也可以使用元组而不是可变参数模板。如果需要,我可以使用最现代的编译器/cpp 标准。

c++ compile-time variadic-templates c++17 c++20

2
推荐指数
1
解决办法
159
查看次数

标签 统计

c++ ×1

c++17 ×1

c++20 ×1

compile-time ×1

variadic-templates ×1