Title
Unevaluated uses of non-static class members
Status
cd1
Section
12.2 [class.mem]
Submitter
Herb Sutter

Created on 2006-10-28.00:00:00 last changed 141 months ago

Messages

Date: 2007-04-15.00:00:00

[Voted into WP at April, 2007 meeting.]

Date: 2007-04-15.00:00:00

Proposed resolution (April, 2007):

The wording is given in paper J16/07-0113 = WG21 N2253.

Date: 2006-10-28.00:00:00

According to 12.2 [class.mem] paragraph 9, the name of a non-static data member can only be used with an object reference (explicit or implied by the this pointer of a non-static member function) or to form a pointer to member. This restriction applies even in the operand of sizeof, although the operand is not evaluated and thus no object is needed to perform the operation. Consequently, determining the size of a non-static class member often requires a circumlocution like

    sizeof ((C*) 0)->m

instead of the simpler and more obvious (but incorrect)

    sizeof (C::m)

The CWG considered this question as part of issue 198 and decided at that time to retain the restriction on consistency grounds: the rule was viewed as applying uniformly to expressions, and making an exception for sizeof would require introducing a special-purpose “wart.”

The issue has recently resurfaced, in part due to the fact that the restriction would also apply to the decltype operator. Like the unary & operator to form a pointer to member, sizeof and decltype need neither an lvalue nor an rvalue, requiring solely the declarative information of the named operand. One possible approach would be to define the concept of “unevaluated operand” or the like, exempt unevaluated operands from the requirement for an object reference in 12.2 [class.mem] paragraph 9, and then define the operands of these operators as “unevaluated.”

History
Date User Action Args
2008-10-05 00:00:00adminsetstatus: wp -> cd1
2007-08-05 00:00:00adminsetstatus: dr -> wp
2007-05-06 00:00:00adminsetmessages: + msg1507
2007-05-06 00:00:00adminsetmessages: + msg1506
2007-05-06 00:00:00adminsetstatus: open -> dr
2006-10-28 00:00:00admincreate