這意味着你的類不是一個字面類型...這個程序是無效的,因爲Options
不是一個文字類類型。但是Checker
是一種文字類型。
struct Checker {
constexpr bool isChecked() {
return false;
}
};
struct Options {
Options(Checker *ConcurrentGBx)
:ConcurrentGBx(ConcurrentGBx)
{ }
constexpr bool is_concurrency_selected()const
{
//GBx is a groupbox with checkbox
return ConcurrentGBx->isChecked();
}
Checker *ConcurrentGBx;
};
int main() {
static Checker c;
constexpr Options o(&c);
constexpr bool x = o.is_concurrency_selected();
}
鏘打印
test.cpp:12:18: error: non-literal type 'Options' cannot have constexpr members
constexpr bool is_concurrency_selected()const
^
test.cpp:7:8: note: 'Options' is not literal because it is not an aggregate and has no constexpr constructors other than copy or move constructors
struct Options {
如果解決這個問題,使Options
構造constexpr
,我的例子片斷編譯。類似的東西可能適用於您的代碼。
你似乎不明白constexpr
是什麼意思。我建議讀一本關於它的書(如果這本書已經存在,無論如何)。
'返回ConcurrentGBx-> isChecked()'是否也是一個constexpr?什麼編譯器在什麼OS上? – RedX 2012-03-07 18:49:25
爲什麼在函數後添加const? 它已經是一個constexpr – TimKouters 2012-03-07 18:49:01