3

私は持っています:

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

理由について何か考えはありますか?

4

1 に答える 1

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です。それについての本を読むことをお勧めします (そのような本がすでに存在する場合)。

于 2012-03-07T21:10:52.850 に答える