ошибка constexpr и bizzare - PullRequest
       4

ошибка constexpr и bizzare

3 голосов
/ 07 марта 2012

У меня:

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

Есть мысли о том, почему?

1 Ответ

6 голосов
/ 08 марта 2012

Это означает, что ваш класс не является литеральным типом ... Эта программа недопустима, потому что 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();
}

Clang печатает

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.Я рекомендую прочитать книгу об этом (если такая книга уже существует, в любом случае).

...