c++98中的常量表达式函数

constant expression function in c++98

我在使用 c++98 常量表达式时遇到以下问题。 这是模板结构的示例..它将在编译时接收大小..

是否有可能在没有 c++11 constexpr 的情况下将此大小作为常量表达式获取? 看看 GetCount()...

    template <typename ValueType, UInt32 size>
    struct FixedArray
    {
       ValueType mArr[size > 0 ? size : 1];
       UInt32 GetCount() const { return size; }

      ...
      other code
      ..
    }

我希望能够做这样的事情:

FixedArray<int , 10> a;
FixedArray<int , a.GetSize()> b;

编辑:

我找不到 C++ 98 的方法,似乎根本不可能。

您可以使用老式的 enum 元编程技巧:

template <typename ValueType, UInt32 size>
struct FixedArray
{
    enum {value = size};
    // All your other stuff, but ditch your GetCount() function.
};

然后,

FixedArray<int, 10> a;
FixedArray<int, a.value> b;

将在 C++98 下编译。

似乎在 C++ 98 中没有办法做到这一点,它根本行不通。