An issue about pointer type that is expected as the type of an operand of the operator prefix++ (also for postfix ++)

Viewed 90

What types will be expected to be the type of operands of prefix++ is defined as the following:

The type of the operand shall be an arithmetic type other than cv bool, or a pointer to a completely-defined object type.

The formal definition for wording object type is defined as: basic.types#general-8

An object type is a (possibly cv-qualified) type that is not a function type, not a reference type, and not cv void.

However, consider the below code:

#include <iostream>
struct Test{
  int a;
};
int main(){
 int Test::* member_ptr = &Test::a;
 ++member_ptr;  // Permite this operation?
}

According to the definition of object type, member type is not be excluded by it. Does it mean we could consider member type to be an object type? you will say, members are not objects, so the pointer to member cannot be used as the operand of prefix++. But, I will argue here, Is a function an object? That's absolutely not, why function type is excluded by the definition for object type, also reference type. So, Is it a defect that didn't place a member type to the exclusive set?

UPDATE

The answers from M.M quotes the rule:

Except for pointers to static members, text referring to “pointers” does not apply to pointers to members.

I know that rule. However, I thought the pointers just refers to literal pointers. After all, the text pointers has appeared in the standard. Such as: expr.eq#2 Comparing pointers is defined as follows. (The rule can absolutely be applied here, that is, does not apply to pointers to members). However, I don't think the text pointers also denotes the form pointer to X(where X is anything other than member,e.g object, function). Is it necessary to clarify that rule?

Except describes anywhere, pointer to X(where X may be object, function, or type) or pointers does not apply to pointer to member.

2 Answers

See [basic.compound]/3:

Except for pointers to static members, text referring to “pointers” does not apply to pointers to members.

So in the first text you quote, "pointer to a completely-defined object type" excludes the possibility of pointers to members. So ++ cannot be applied to a pointer-to-member.

Whenever my kids have trouble understanding a complex mathematical expression I tell them this: OK, you know that this expression is all about numbers. So try and substitute some numbers for certain sub-expressions, and see if that makes it easier to operate on it. Once you get comfortable with 5 in place of sin(x) and 5/10 in place of tan(x), then switch back. I've had considerable success with this when fancier explanatory options have ran out. (I can't think of any actual examples of formulas we used that on - sorry, I'll edit the question later, potentially months or years later, when something comes to mind).

So, let's do this with the C++ standard. Per [basic.compound]/3:

Except for blue speckled eggs, text referring to “eggs” does not apply to blue eggs.

Then you quote:

The type of the operand shall be an arithmetic type other than cv bool, or a colorful egg.

That egg cannot be blue, unless of course it'd be literally stated to apply to blue eggs.

However, I don't think the text eggs also denotes the form egg of color X.

Well. If this was about eggs, you would think otherwise, right? And there is clearly a decent homomorphism between and poultry husbandry and C++ :)

  • Egg = pointer
  • Blue egg = pointer to a member (in general)
  • Blue speckled egg = pointer to a static member
  • Colorful egg = pointer to a fully defined object type
  • Plain (colorless egg = pointer to a not fully defined object type

Sorta-kinda like that :)

Sometimes, it really is worth substituting homomorphic classes of objects that you're familiar with (I assume you're familiar with eggs, if not - pencils work well, too). Things just don't sound so weird then, and I completely appreciate that it's hard to think of the C++ standard as if it pertained to some familiar objects. Just pretend you're familiar with them and keep going.

Related