Why does the compiler complain about this not being a constexpr?

心已入冬 提交于 2019-12-04 04:28:13

[basic.types]/p10 states that:

A type is a literal type if it is:

  • possibly cv-qualified void; or

  • a scalar type; or

  • a reference type; or

  • an array of literal type; or

  • a possibly cv-qualified class type (Clause [class]) that has all of the following properties:

    • it has a trivial destructor,

    • it is either a closure type ([expr.prim.lambda]), an aggregate type ([dcl.init.aggr]), or has at least one constexpr constructor or constructor template (possibly inherited ([namespace.udecl]) from a base class) that is not a copy or move constructor,

    • if it is a union, at least one of its non-static data members is of non-volatile literal type, and

    • if it is not a union, all of its non-static data members and base classes are of non-volatile literal types.

where [class.dtor]/p5 says that:

A destructor is trivial if it is not user-provided and if:

(5.4) — the destructor is not virtual,

(5.5) — all of the direct base classes of its class have trivial destructors, and

(5.6) — for all of the non-static data members of its class that are of class type (or array thereof), each such class has a trivial destructor.

Otherwise, the destructor is non-trivial.

In other words, to declare a constexpr instance of Matrix, it must be a literal type, and to be a literal type, its destructor must be either defaulted, or removed altogether, so:

~Matrix() = default;

or:

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