c++ - 如何在 c++17 中生成内联 constexpr 普通数组数组
问题描述
问题
我正在用 c++17 编写一个性能绝对关键的软件。我想在数组本身的数组中提供一些关键函数常量。重要的是,这两个数组都可以通过整数值以这种(或类似)方式访问:int main()
{
for (int i = 0; i < size_of_A ; i++)
{
for (int j = 0; j < size_of_B_in_A(i); j++)
{
std::cout << A[i][j];
}
}
}
假设某些函数,这将是我们想要创建的那种数组int f(a, b)
A
{
// B1
{
f(1, 1),
f(1, 2),
f(1, 3),
...
f(1, large number)
},
// B2
{
f(2, 1),
...
f(2, some other large number)
},
... etc
}
扭曲
每个内部数组可能具有不同的大小,我们将存储在其他地方,我们必须在编译时找到大小。我宁愿不使用 std::vector 因为它们被假定 稍慢 。另外,我认为 std::vector 将存储在堆上,这在我的特定情况下将是一个性能问题。此外, std::vector 不能用作“内联 constexpr” ,这是必要的,因为我希望这些数组中的大量值永远不会改变。我可以每次都重新编译所有这些值,但不要按照策略将它们保存在外部文件中,因为我要遵循严格的编码风格。我试过的
大括号初始化器
// A.hh
#pragma once
#include <iostream>
void test1();
void test2();
inline constexpr int B1[1] = {1};
inline constexpr int B2[2] = {2, 3};
inline constexpr int B3[3] = {4, 5, 6};
inline constexpr const int *A[3] = {B1, B2, B3};
// main.cc
#include "A.hh"
int main()
{
std::cout << "values : ";
for (int i = 0; i < 3; i++)
{
for (int j = 0; j <= i; j++)
{
std::cout << A[i][j];
}
}
std::cout << "\n\naddress test : \n";
std::cout << &A << '\n';
test1();
test2();
}
// somewhere.cc
#include "A.hh"
void test1()
{
std::cout << &A << '\n';
}
// elsewhere.cc
#include "A.hh"
void test2()
{
std::cout << &A << '\n';
}
打印:
./a.out
values : 123456
address test :
0x56180505cd70
0x56180505cd70
0x56180505cd70
因此 A 没有被复制到 main.cc、somewhere.cc 和 else.cc 中,这很好。我想走得更远,能够创造大量的价值。
带有 constexpr 的结构
使用 此处 找到的提示,我这样做是为了能够在数组构造期间执行操作。// B.hh
#pragma once
#include <iostream>
template <int N>
struct X
{
int arr[N];
constexpr X(): arr()
{
for (int i = 0; i < N; i++)
{
arr[i] = i % 3;
}
}
};
inline constexpr auto A = X<500>();
// main.cc
#include "B.hh"
int main()
{
for (int i = 0; i < 500; i++)
{
std::cout << A.arr[i];
}
}
毫无疑问地打印出来
012012 (etc)...
最后是数组数组
这就是我卡住的地方#pragma once
#include <iostream>
template <int N>
struct sub_array
{
int arr[N];
constexpr sub_array() : arr()
{
for (int i = 0; i < N; i++)
{
arr[i] = i;
}
}
};
struct array
{
sub_array</*what here ?*/> arr[100];
constexpr array() : arr()
{
for (int i = 0; i < 100; i++)
{
int size = i * 2; // a very large number
// the value of 'size' is not usable in a constant expression
//
// I see why it is, but I can't think of any other way
arr[i] = sub_array<size>;
}
}
};
inline constexpr array A = array();
我怎样才能建立这样的阵列?
感谢您的时间和考虑。
解决方案
只需使用std::array<std::span<int>, N>
,它是一个由不同大小的 span 组成的固定大小的数组。要生成它,请使用std::index_sequence
标题:
constexpr std::size_t size_of_A = 500;
extern const std::array<const std::span<const int>, size_of_A>& A;
执行:
constexpr std::size_t size_of_B_in_A(std::size_t i) { return i%10+1;}
constexpr int f(std::size_t i, std::size_t j) {return static_cast<int>(i%(j+1));}
template <int I, int N>
struct B
{
std::array<int,N> arr;
explicit constexpr B()
{
for (int j = 0; j < N; j++)
arr[j] = f(I, j);
}
constexpr operator const std::span<const int>() const {return {arr};}
};
template<class index_sequence>
class BGen;
template<std::size_t... I>
struct BGen<std::integer_sequence<std::size_t,I...>> {
static constexpr std::tuple<B<I, size_of_B_in_A(I)>...> bs{};
static constexpr std::array<const std::span<const int>, sizeof...(I)> A {std::get<I>(bs)...};
};
const std::array<const std::span<const int>, size_of_A>& A
= BGen<decltype(std::make_index_sequence<size_of_A>{})>::A;
用法:
int main()
{
for (unsigned i = 0; i < A.size() ; i++)
{
for (unsigned j = 0; j < A[i].size(); j++)
{
std::cout << A[i][j];
}
}
}
http://coliru.stacked-crooked.com/a/d68b0e9fd6142f86
但是,退后一步:这个解决方案不是解决这个问题的正常方法。既然都是constexpr
,这都是数据而不是代码。因此,最高效的解决方案是两个程序。生成数据并将其保存到程序附带的(内部?)文件中。然后您的程序只需将文件映射到内存中,并直接使用数据。
推荐阅读
- azure-pipelines - Azure Pipelines - 管道工件和构建工件有什么区别?
- python-3.x - 没有得到输出,但他们没有显示错误
- php - 如何从输入请求中返回多个视图变量?
- html - 如何从 Angular Material Datepicker 中仅提取日期
- sql-server - 创建动态数据透视表列名
- javascript - 如何转换一个计数到 HH:MM:SS 的数字?
- mysql - MySql 和 JDBC:将单个列的字符集更改为 utf8mb4
- javascript - 进口订单的履行情况?
- python - 如何从我的 pipelines.py 文件中导入我的蜘蛛类的变量?
- r - 引用 DF 中预览行的条件公式不起作用