Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
78 views
in Technique[技术] by (71.8m points)

c++ - Is class member access through a pointer to data member considered a member access expression?

One of the rules explaining how the decltype keyword works states that:
If the argument is [...] an unparenthesized class member access expression, then decltype yields the type of the entity named by this expression. (https://en.cppreference.com/w/cpp/language/decltype)
However, consider the following example (pseudo-code for brevity):

struct X {
    int a;
};

X x;
int X::* ptr = &X::a;
std::is_lvalue_reference<decltype(x.*ptr)>::value; //Evaluates to 1. The type is int&
std::is_rvalue_reference<decltype(X().*ptr)>::value; //Evaluates to 1. The type is int&&

This implies that accessing a class member through a pointer to member is not a class member access expression. It's treated as if it was just a normal expression, for which the rule I quoted doesn't apply.
Therefore, my question is: how can I know what exactly counts as a class member access expression for the purpose of decltype? What source should I look into to find a precise definition?

question from:https://stackoverflow.com/questions/65939553/is-class-member-access-through-a-pointer-to-data-member-considered-a-member-acce

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Class member access is defined in [expr.ref].

It is:

A postfix expression followed by a dot . or an arrow ->, optionally followed by the keyword template, and then followed by an id-expression

x.y is class member access, get().y is class member access, x.template y<int>() is class member access, etc.

But neither x.*ptr nor X().*ptr are, because of the *. They are pointer-to-member operators, which are described in [expr.mptr.oper]. They also have different (lower) precedence from class member access: x.*y.z evaluates as x.*(y.z).


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...