由于 ibm i 上的特定 IO 进程,需要使用显示文件字段 IO。
如下所示,我们需要编译时结构来显示文件值。
看了之后constexpr
我决定尝试一些来自 here 的 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
关键字在这种情况下,但为什么呢?
我是不是在代码的某处搞砸了,或者这种用法目前不受支持?
Here是 ibm 编译器支持的功能,IBM XLC++ 支持 constexpr
到目前为止,我可以从给定的表格中扣除。
最佳答案
鉴于它被标记为 ibm-midrange
,我不确定 IBM XLC++ 的功能集是否是正确的使用引用。我想你想使用 this反而。特别是如果您想使用 C++0x 功能(尚未完全支持),您需要使用 LANGLVL(*EXTENDED0X)
进行编译。
欲了解更多信息,this link显示有关支持 ILE C++0x 语言扩展的信息。
关于c++ - ibm i 7.3 上初始化 constexpr 编译时数组时出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53259912/