Title
Inconsistent restrictions for static_cast on pointers to out-of-lifetime objects
Status
tentatively ready
Section
6.8.4 [basic.life]
Submitter
Cody Miller

Created on 2025-02-26.00:00:00 last changed 2 weeks ago

Messages

Date: 2025-09-26.20:51:59

Proposed resolution (approved by CWG 2025-09-26):

  1. Change 7.3.12 [conv.ptr] paragraph 3 as follows:

    ... Otherwise, if B is a virtual base class of D or is a base class of a virtual base class of D and v does not point to an object whose type is similar (7.3.6 [conv.qual]) to D and that is within its lifetime or within its period of construction or destruction (11.9.5 [class.cdtor]), the behavior is undefined. ...
  2. Change in 6.8.4 [basic.life] paragraph 7 as follows:

    Before the lifetime of an object has started but after the storage which the object will occupy has been allocated [ Footnote: ...] ... The program has undefined behavior if
    • the pointer is used as the operand of a delete-expression,
    • the pointer is used to access a non-static data member or call a non-static member function of the object, or
    • the pointer is implicitly converted (7.3.12 [conv.ptr], 7.6.1.9 [expr.static.cast]) to a pointer to a virtual base class or to a base class thereof, or
    • the pointer is used as the operand of a static_cast (7.6.1.9 [expr.static.cast]), except when the conversion is to pointer to cv void, or to pointer to cv void and subsequently to pointer to cv char, cv unsigned char, or cv std::byte (17.2.1 [cstddef.syn]), or
    • the pointer is used as the operand of a dynamic_cast (7.6.1.7 [expr.dynamic.cast]).
Date: 2025-09-26.20:51:59

According to 6.8.4 [basic.life] paragraph 7, converting a pointer-to-derived to pointer-to-base for an out-of-lifetime object is allowed implicitly, but disallowed using static_cast. That seems inconsistent.

Furthermore, base classes of virtual base classes should get the same treatment as virtual base classes themselves.

History
Date User Action Args
2025-09-26 20:51:59adminsetmessages: + msg8127
2025-09-26 20:51:59adminsetstatus: review -> tentatively ready
2025-09-13 07:51:41adminsetstatus: open -> review
2025-02-26 00:00:00admincreate