Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
55 views
in Technique[技术] by (71.8m points)

c++ - Why can't a destructor be marked constexpr?

In C++, you can declare many things as constexpr: variables, functions (including member functions and operators), constructors, and since C++1z, also if statements and lambda expressions. However, declaring a destructor constexpr results in an error:

struct X {
    constexpr ~X() = default; // error: a destructor cannot be 'constexpr'
};

My questions:

  1. Why can't a destructor be marked constexpr?
  2. If I do not provide a destructor, is the implicitly generated destructor constexpr?
  3. If I declare a defaulted destructor (~X() = default;), is it automatically constexpr?
question from:https://stackoverflow.com/questions/45071876/why-cant-a-destructor-be-marked-constexpr

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

As per the draft basic.types#10 possibly cv-qualified class type that has all of the following properties:

A possibly cv-qualified class type that has all of the following properties:

(10.5.1) - it has a trivial destructor,

(10.5.2) - it is either a closure type, an aggregate type, or has at least one constexpr constructor or constructor template (possibly inherited from a base class) that is not a copy or move constructor,

(10.5.3) - if it is a union, at least one of its non-static data members is of non-volatile literal type

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

Ques 1: Why a destructor cannot be marked as constexpr?

Because only trivial destructors are qualified for constexpr Following is the relevant section of the draft

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.

Ques 2: If I do not provide a destructor, is the implicitly generated destructor constexpr?

Yes, because implicitly generated destructor is trivial type, so it is qualified for constexpr

Ques 3: If I declare a defaulted destructor (~X() = default;), is it automatically constexpr?

Indeed, this destructor is user-declared and implicitly-generated and thus it is qualified for constexpr.


I'm not able to find any direct reference that only trivial destructors are qualified for constexpr but if the destructor is not trivial then it is for sure that class type is not cv-qualified. So it kind of implicit as you can't define a destructor for cv-qualified class.


C++20 Update

Since C++20, user defined destructors can also be constexpr under certain conditions.

dcl.constexpr/3:

The definition of a constexpr function shall satisfy the following requirements:

  • its return type (if any) shall be a literal type;
  • each of its parameter types shall be a literal type;
  • it shall not be a coroutine ([dcl.fct.def.coroutine]);
  • if the function is a constructor or destructor, its class shall not have any virtual base classes;
  • its function-body shall not enclose ([stmt.pre])
    • a goto statement,
    • an identifier label ([stmt.label]),
    • a definition of a variable of non-literal type or of static or thread storage duration.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

57.0k users

...