constexpr和bizzare错误

sma*_*llB 3 c++ constexpr c++11

我有:

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

我收到错误:

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

有什么想法吗?

Joh*_*itb 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();
}
Run Code Online (Sandbox Code Playgroud)

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 {
Run Code Online (Sandbox Code Playgroud)

如果您修复此问题并创建Options构造函数constexpr,我的示例代码段将进行编译.类似的东西可能适用于您的代码.

你似乎不明白是什么constexpr意思.我建议你读一本关于它的书(如果这本书已经存在,无论如何).