Title
consteval defaulted functions
Status
c++23
Section
9.2.6 [dcl.constexpr]
Submitter
Aaron Ballman

Created on 2022-06-16.00:00:00 last changed 9 months ago

Messages

Date: 2023-01-27.23:50:39

Proposed resolution (approved by CWG 2023-01-27):

  1. Change in 9.2.6 [dcl.constexpr] paragraph 3 as follows:

    The definition of a constexpr function shall satisfy the following requirements: A function is constexpr-suitable if:
    • it shall is not be a coroutine (9.5.4 [dcl.fct.def.coroutine]); , and
    • if the function is a constructor or destructor, its class shall does not have any virtual base classes.
    Except for instantiated constexpr functions, non-templated constexpr functions shall be constexpr-suitable.
  2. Delete 9.2.6 [dcl.constexpr] paragraph 4:

    If the instantiated template specialization of a constexpr function template or member function of a class template would fail to satisfy the requirements for a constexpr function, that specialization is still a constexpr function, even though a call to such a function cannot appear in a constant expression.
  3. Change in 7.5.5.2 [expr.prim.lambda.closure] paragraph 5 as follows:

    ... The function call operator or any given operator template specialization is a constexpr function if either the corresponding lambda-expression's parameter-declaration-clause is followed by constexpr or consteval, or it satisfies the requirements for a constexpr function is constexpr-suitable (9.2.6 [dcl.constexpr]). ...
  4. Change in 7.7 [expr.const] bullet 5.5 as follows:

    • an invocation of an instantiated constexpr function that fails to satisfy the requirements for a constexpr function is not constexpr-suitable;
  5. Change in 9.5.2 [dcl.fct.def.default] paragraph 3 as follows:

    A function explicitly defaulted on its first declaration is implicitly inline (9.2.8 [dcl.inline]), and is implicitly constexpr (9.2.6 [dcl.constexpr]) if it satisfies the requirements for a constexpr function is constexpr-suitable.
  6. Change in 11.4.7 [class.dtor] paragraph 9 as follows:

    A defaulted destructor is a constexpr destructor if it satisfies the requirements for a constexpr function is constexpr-suitable (9.2.6 [dcl.constexpr]).
Date: 2022-11-15.00:00:00

Additional notes (November, 2022)

The proposed wording is possibly not addressing the point of the issue; the issue has been retracted from the WG21 plenary straw polls for further consideration in CWG.

Date: 2022-08-15.00:00:00

Proposed resolution (August, 2022) [SUPERSEDED]:

Change in 9.2.6 [dcl.constexpr] paragraph 4 as follows:

If the instantiated template specialization of a constexpr templated function template or member function of a class template would fail to satisfy the requirements for a constexpr function, that specialization is still a constexpr function, even though a call to such a function cannot appear in a constant expression. Similarly, if the instantiated template specialization of a consteval templated function would fail to satisfy the requirements for a consteval function, that specialization is still an immediate function, even though an immediate invocation would be ill-formed.
Date: 2023-02-15.00:00:00

[Accepted as a DR at the February, 2023 meeting.]

It is not clear whether a defaulted consteval function is still an immediate function even if it is not a valid constexpr function. For example:

  template <typename Ty>
  struct A {
    Ty n;
    consteval A() {}
  };

  template <typename Ty>
  struct B {
    Ty n;
    consteval B() = default;
  };

  A<int> a;
  B<int> b;

The declarations of a and b should both fail due to an uninitialized member n in each of A and B. The = default; should not make a difference. However, there is implementation divergence. We should be able to lean on 7.7 [expr.const] bullet 5.5 to handle this when the immediate invocation is required.

Possible resolution:

Change in 9.2.6 [dcl.constexpr] paragraph 7 as follows:

If the instantiated template specialization of a constexpr templated function template or member function of a class template would fail to satisfy the requirements for a constexpr function, that specialization is still a constexpr function, even though a call to such a function cannot appear in a constant expression. Similarly, if the instantiated template specialization of a consteval templated function would fail to satisfy the requirements for a consteval function, that specialization is still an immediate function, even though an immediate invocation would be ill-formed. If no specialization of the template would satisfy the requirements for a constexpr or consteval function when considered as a non-template function, the template is ill-formed, no diagnostic required.
History
Date User Action Args
2023-07-16 13:00:43adminsetstatus: open -> c++23
2023-07-16 13:00:43adminsetstatus: dr -> open
2023-02-18 18:43:04adminsetstatus: ready -> dr
2023-02-07 14:43:26adminsetstatus: tentatively ready -> ready
2023-01-27 23:50:39adminsetstatus: review -> tentatively ready
2023-01-07 20:01:16adminsetmessages: + msg7122
2022-11-25 05:14:04adminsetmessages: + msg7061
2022-11-25 05:14:04adminsetstatus: ready -> review
2022-11-09 21:38:30adminsetstatus: tentatively ready -> ready
2022-08-19 07:54:33adminsetmessages: + msg6892
2022-08-19 07:54:33adminsetstatus: open -> tentatively ready
2022-06-16 00:00:00admincreate