[英]Can I use a constexpr function to run an algorithm on an array at compile time?
[英]Error on initialization constexpr compile time array on ibm i 7.3
由於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編譯器支持的功能,而IBM XLC ++支持constexpr
(據我可以從給定的表中得出)。
我無法在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;
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.