English 中文(简体)
编译时常数数组放在哪里?
原标题:Where to put compile-time-constant arrays?

假设我有一个数组存储前10个素数,如下所示:

const int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};

只要我有1.cpp文件,这一切都非常好和简单。但是,如果我有多个.cpp文件,我真的不知道该把这个数组放在哪里。

一个显而易见的解决方案是:

// primes.h:
extern const int primes[10];

// primes.cpp:
extern const int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};

然而,这样做的问题在于素数数组不再是编译时常数。假设x.cpp想做一些涉及素数[k]的繁重计算,如果k是编译时间常数,它必须进行实际的内存查找。我不喜欢那样。

那么我该把这个数组放在哪里,这样:

  1. It s only once in the binary (not once per .cpp file)
  2. array[SOME_CONSTANT] is also a compile-time constant

Edit

这个怎么样?

inline int prime(int i) {
    static const int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};
    return primes[i];
}

附言:即使是上面的“显而易见的解决方案”也花了我相当长的时间来写。显然,默认情况下const变量有内部链接,所以我不得不在primes.cpp文件中添加“extern”以使其工作。

最佳回答

我认为这应该有效(现在在Migi的测试发现缺陷后更新):

template <bool dummy>
struct primes_lut
{
    static const int values[];
};

template<bool dummy>
const int primes_lut<dummy>::values[] = { 2, 3, 5, 7, 11, 13, 17, 19, 23, 29 };

static auto& primes = primes_lut<true>::values;

(C++中没有使用更多模板无法解决的问题。)

另一种方法:

struct primes_lut { int values[10]; };
inline const primes_lut& primes_lut_provider(void)
{
    static const primes_lut values = { {2, 3, 5, 7, 11, 13, 17, 19, 23, 29} };
    return values;
}
static const int (&primes)[10] = primes_lut_provider().values;

最后,对于现代链接器来说,这些技巧都不是必需的,只有实现恒定折叠。

问题回答

您可以在标头中使用enum。枚举保证是编译时常量,并且(除非使用C++0X枚举类)隐式转换为整数。

static const int primes[] = {2, 3, 5, 7, 11, 13, 17, 19, 23, 29};

在头文件中。这可能会导致更大的可执行文件(每个源文件都有自己的数组副本),但我认为同样的问题也适用于当前接受的答案。





相关问题
Undefined reference

I m getting this linker error. I know a way around it, but it s bugging me because another part of the project s linking fine and it s designed almost identically. First, I have namespace LCD. Then I ...

C++ Equivalent of Tidy

Is there an equivalent to tidy for HTML code for C++? I have searched on the internet, but I find nothing but C++ wrappers for tidy, etc... I think the keyword tidy is what has me hung up. I am ...

Template Classes in C++ ... a required skill set?

I m new to C++ and am wondering how much time I should invest in learning how to implement template classes. Are they widely used in industry, or is this something I should move through quickly?

Print possible strings created from a Number

Given a 10 digit Telephone Number, we have to print all possible strings created from that. The mapping of the numbers is the one as exactly on a phone s keypad. i.e. for 1,0-> No Letter for 2->...

typedef ing STL wstring

Why is it when i do the following i get errors when relating to with wchar_t? namespace Foo { typedef std::wstring String; } Now i declare all my strings as Foo::String through out the program, ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

Window iconification status via Xlib

Is it possible to check with the means of pure X11/Xlib only whether the given window is iconified/minimized, and, if it is, how?

热门标签