This is an unofficial snapshot of the ISO/IEC JTC1 SC22 WG21 Core Issues List revision 110b. See http://www.open-std.org/jtc1/sc22/wg21/ for the official list.

2023-01-15


1306. Modifying an object within a const member function

Section: _N4868_.11.4.3.2  [class.this]     Status: CD3     Submitter: James Kanze     Date: 2011-04-26

[Voted into the WP at the February, 2012 meeting; moved to DR at the October, 2012 meeting.]

According to _N4868_.11.4.3.2 [class.this] paragraph 2,

In a const member function, the object for which the function is called is accessed through a const access path; therefore, a const member function shall not modify the object and its non-static data members.

This is clearly overstating the case: mutable members can be modified, a const_cast can be used, and class member access expressions not involving this can also allow the object to be modified. The effect of the cv-qualification of a member function on the type of *this is clear from the preceding paragraph; this statement appears both unnecessary and incorrect.

Proposed resolution (August, 2011):

Merge _N4868_.11.4.3.2 [class.this] paragraphs 1 and 2 and change the text as follows:

In the body of a non-static (11.4.2 [class.mfct]) member function, the keyword this is a prvalue expression whose value is the address of the object for which the function is called. The type of this in a member function of a class X is X*. If the member function is declared const, the type of this is const X*, if the member function is declared volatile, the type of this is volatile X*, and if the member function is declared const volatile, the type of this is const volatile X*. In [Note: thus in a const member function, the object for which the function is called is accessed through a const access path; therefore, a const member function shall not modify the object and its non-static data members. end note] [Example:...