constexpr and bizzare error

蓝咒 提交于 2019-12-23 19:17:08

问题


I'm having:

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

and I'm getting error:

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

Any thoughts on why?


回答1:


It means your class is not a literal type... This program is invalid, because Options is not a literal class type. But Checker is a literal type.

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 prints

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 {

If you fix this and make the Options constructor constexpr, my example snippet compiles. Similar things may apply to your code.

You appear to not understand what constexpr means. I recommend reading a book about it (if such a book exists already, anyway).



来源:https://stackoverflow.com/questions/9607279/constexpr-and-bizzare-error

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!