Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/126.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ c+中的常量表达式函数+;98_C++_Templates_C++98_Compile Time Constant_Constant Expression - Fatal编程技术网

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

C++ c+中的常量表达式函数+;98,c++,templates,c++98,compile-time-constant,constant-expression,C++,Templates,C++98,Compile Time Constant,Constant Expression,我对c++98常量表达式有以下问题。 下面是一个模板结构的示例。。它将在编译时接收大小 在没有c++11 constexpr的情况下,是否有可能将这个大小作为常量表达式? 请看GetCount() 模板 结构固定阵列 { ValueType mArr[大小>0?大小:1]; UInt32 GetCount()常量{返回大小;} ... 其他代码 .. } 我希望能够做到以下几点: FixedArray<int , 10> a; FixedArray<int , a.GetSi

我对c++98常量表达式有以下问题。 下面是一个模板结构的示例。。它将在编译时接收大小

在没有c++11 constexpr的情况下,是否有可能将这个大小作为常量表达式? 请看GetCount()

模板
结构固定阵列
{
ValueType mArr[大小>0?大小:1];
UInt32 GetCount()常量{返回大小;}
...
其他代码
..
}
我希望能够做到以下几点:

FixedArray<int , 10> a;
FixedArray<int , a.GetSize()> b;
固定阵列a;
固定阵列b;
编辑:


<>我找不到C++ 98的方法,好像根本不可能。

你可以使用旧式的<代码> EnUM < /Calp>元编程技巧:

template <typename ValueType, UInt32 size>
struct FixedArray
{
    enum {value = size};
    // All your other stuff, but ditch your GetCount() function.
};
模板
结构固定阵列
{
枚举{值=大小};
//所有其他东西,但放弃GetCount()函数。
};
那么

固定阵列a;
固定阵列b;

将在C++ 98下编译。

< P>似乎在C++ 98中没有办法做到这一点,它根本不起作用。看到了吗,现在人们正在C++17上跳跃。为什么要坚持一个已经过时了19年的标准?@DeiDei:大型软件栈是升级的障碍。但是,是的,现在至少应该使用C++03了。两年前我搬到了C++11。我将在2020的某个时间安排一个移动到C++ 17,完全跳过C++ 14。在DeiDei,我们必须支持现代和C++。98@Bathsheba也许吧,但我不是真的买它。打开C++14编译器标志重新编译旧项目可能不会破坏太多东西(如果有的话)。在合理的短时间内,至少不会有太多的问题需要解决。还有没有办法保持GetCount函数?你可以让它
返回值但不能在编译时使用它。
enum
方法几乎为您提供了一个
constexpr
UInt32 GetCount()const{return value;}
将。它仍然表示“类型为'UInt32'(也称为'unsigned int')的非类型模板参数不是整型常量表达式”,您何时使用该函数?
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;