2012-03-07 45 views
3

我有:constexpr的和古怪的錯誤

constexpr bool is_concurrency_selected()const 
    { 
     return ConcurrentGBx->isChecked();//GBx is a groupbox with checkbox 
    } 

和我得到的錯誤:

C:\...\Options_Dialog.hpp:129: error: enclosing class of 'bool Options_Dialog::is_concurrency_selected() const' is not a literal type 

任何想法,爲什麼?

+0

'返回ConcurrentGBx-> isChecked()'是否也是一個constexpr?什麼編譯器在什麼OS上? – RedX 2012-03-07 18:49:25

+0

爲什麼在函數後添加const? 它已經是一個constexpr – TimKouters 2012-03-07 18:49:01

回答

6

這意味着你的類不是一個字面類型...這個程序是無效的,因爲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是什麼意思。我建議讀一本關於它的書(如果這本書已經存在,無論如何)。