假设我有下面的C++代码
template <int Id>
struct Foo {
static inline int x {};
};
int main() {
using T0 = Foo<0>;
using T1 = Foo<1>;
...
using T999 = Foo<999>;
// Do some stuff with the various x's at runtime
int result = T0::x + T1::x + ... + T999::x;
}
现在检查最后一行中的总和。是否可以保证各种静态 x 将连续存储在内存中,并且该总和将在缓存方面有效?或者一般来说,我们应该假设静态变量只是以某种随机顺序存储在随机位置,这种方式不会提高缓存效率。
如果是前者,是不是因为using语句是按顺序出现的?模板实例化是因为存在 using 语句而发生还是模板实际上必须被使用(在这种情况下它们可能按使用顺序存储)?
在后者的情况下,使用大小为 1000 的元组并且每个 Foo::x 使用该元组中的第 n 个索引作为其变量可以解决该问题吗?
虽然可能会出现连续分配的情况,但实际上并不能保证。最好创建一个数组并创建常量索引来引用特定片段,例如
const variable7_ind = 7
,然后使用 T[variable7_ind]
代替 T7
。
代码是
#include <iostream>
template <int Idx>
struct Foo {
static inline int x{};
};
template <int Idx>
struct Bar {
static inline int x{};
};
int main() {
using A = Foo<0>;
using B = Foo<1>;
using C = Foo<2>;
using D = Bar<0>;
std::cout << &B::x << std::endl;
std::cout << &C::x << std::endl;
std::cout << &D::x << std::endl;
std::cout << &A::x << std::endl;
}
我的系统是:
Homebrew clang version 17.0.6
Target: arm64-apple-darwin22.1.0
Thread model: posix
输出是
0x100dc4000
0x100dc4004
0x100dc4008
0x100dc400c
由此我们可以得出结论,只要
// Do some stuff with the various x's at runtime
按顺序使用不同的 x,因为这与它们存储在内存中的顺序相同。它基于使用顺序而不是
using
语句 的顺序