在ibmi7.3上初始化constexpr编译时数组时出错



由于ibm i上的特定IO过程,需要使用显示文件字段IO.

如下所示,我们需要显示文件值的编译时结构。

在看了constexpr之后,我决定从这里尝试一些cpp+模板解决方案。

我的案例的最终代码如下:

MYSRC/MYMOD.CPP

#include "MYSRC/MODINCH"
template <int N>
constexpr_string<N> make_constexpr_string(const char(&a)[N]) {
// Provide a function template to deduce N           ^ right here
return constexpr_string<N>(a);
//                     ^ Forward the parameter to the class template.
};
int main(int argc, char** argv)
{
return 0;
}

MYSRC/MODINCH.H

#include <algorithm>
#define __IBMCPP_TR1__ 1
#include <QSYSINC/STD(array)>
using std::size_t;
template <size_t N> // N is the capacity of my string.
class constexpr_string {
private:
//std::tr1::array<char, N> data_; // Reserve N chars to store anything.  
char data_[N];
std::size_t size_;         // The actual size of the string.
public:
constexpr constexpr_string(const char(&a)[N]): data_{}, size_(N - 1)
{
for (std::size_t i = 0; i < N; ++i) {
data_[i] = a[i];
}
}
constexpr iterator begin() {  return data_;   }       // Points at the beggining of the storage.
constexpr iterator end() {  return data_ + size_;   } // Points at the end of the stored string.
};

上面的代码用编译

CRTCPPMOD MODULE(QTEMP/MYMOD) SRCFILE(MYLIB/MYSRC) SRCMBR(MYMOD)
OPTIMIZE(40) DBGVIEW(*ALL) LANGLVL(*EXTENDED0X)

对于char data_[N];std::tr1::array<char, N> data_;

然而,当我尝试像这样填充constexpr_string的实例时:

#include "MYSRC/MODINCH"
template <int N>
constexpr_string<N> make_constexpr_string(const char(&a)[N]) {
// Provide a function template to deduce N           ^ right here
return constexpr_string<N>(a);
//                     ^ Forward the parameter to the class template.
};
int main(int argc, char** argv)
{
auto test1 = make_constexpr_string("blabla");
constexpr_string<7> test("blabla");
return 0;
}

错误立即导致编译失败,并显示此消息CZP0063(30) The text "constexpr_string" is unexpected.就在ctor线上。对我来说,在这种情况下,编译器似乎无法确定constexpr关键字,但为什么呢?

是我在代码中搞砸了什么地方,还是目前不支持这种用法?

以下是ibm编译器和IBMXLC++支持的constexpr的特性,我可以从给定的表中推断出来。

鉴于它被标记为ibm-midrange,我不确定IBM XLC++的功能集是否是正确的参考。我想你应该用这个来代替。特别是,如果您想使用C++0x功能(尚未完全支持(,则需要使用LANGLVL(*EXTENDED0X)进行编译。

有关更多信息,此链接显示了有关支持ILE C++0x语言扩展的信息。

我无法在ibm-midrange系统上验证这一点,因为我无法访问那里的CPP编译器,但我想我发现了您的问题:

#include "MYSRC/MODINCH"
template <int N>
constexpr_string<N> make_constexpr_string;  // <------ You are missing this semicolon
...
int main(int argc, char** argv)
{
auto test1 = make_constexpr_string("blabla");
constexpr_string<7> test("blabla");
return 0;
}

最新更新