0

Assuming a class A has a member variable(which is an object rather than a reference) m. Naturally I will think that: When I defined an object 'o1', then the Expression 'o1.m' is an object type either; When I defined a reference 'q1', then the Expression 'q1.m' is a reference type either.

Is that conclusion correct? I'm wondering what any relative clauses are in C++ standard document if it is true(I've been searching for it/them for one more days already but got almost all about 'reference as member of class' so far)?

  • Could you give a short code sample to illustrate please? – πάντα ῥεῖ Dec 07 '14 at 17:04
  • `Naturally I will think...` And you would be wrong. When `p1` is a pointer to `A`, do you just as naturally think that `p1->m` is a pointer? In any case, for all intents and purposes, there ain't no such thing as an expression of reference type: **5/5** If an expression initially has the type “reference to `T`” (8.3.2, 8.5.3), the type is adjusted to `T` prior to any further analysis. – Igor Tandetnik Dec 07 '14 at 17:04
  • 'an object rather than a reference', this comparison doesn't make sense – senex Dec 07 '14 at 17:05
  • To make this knowledge systematic you need to understand lvalue/rvalue and C++ value categories overall: http://en.cppreference.com/w/cpp/language/value_category – Alexander V Dec 07 '14 at 17:06
  • Assuming i care, you're assumption is not correct. – Captain Obvlious Dec 07 '14 at 17:08
  • don't tell a story about how a could wold be. Give a simple code example. – bolov Dec 07 '14 at 17:08
  • @bolov What example code should help for solving misconceptions? – πάντα ῥεῖ Dec 07 '14 at 17:45
  • @Alex720 I'm making some traits which might base on such kind of conclusions. – unituniverse Dec 07 '14 at 18:23
  • @IgorTandetnik One my friend asked me that days ago and I realized that I've never thought it clearly. Fortunately no work I've made before relying on such kind of conclusions so far. – unituniverse Dec 07 '14 at 18:39

1 Answers1

2

Take a look at §5.2.5 (Class member access in Postfix Expressions, from N3797 C++14 Draft):

Abbreviating postfix-expression.id-expression as E1.E2, E1 is called the object expression. The type and value category of E1.E2 are determined as follows. In the remainder of 5.2.5, cq represents either const or the absence of const and vq represents either volatile or the absence of volatile. cv represents an arbitrary set of cv-qualifiers, as defined in 3.9.3.

If E2 is declared to have type “reference to T,” then E1.E2 is an lvalue; the type of E1.E2 is T. Otherwise, one of the following rules applies.

  • If E2 is a non-static data member and the type of E1 is “cq1 vq1 X”, and the type of E2 is “cq2 vq2 T”, the expression designates the named member of the object designated by the first expression. If E1 is an lvalue, then E1.E2 is an lvalue; otherwise E1.E2 is an xvalue. Let the notation vq12 stand for the “union” of vq1 and vq2 ; that is, if vq1 or vq2 is volatile, then vq12 is volatile. Similarly, let the notation cq12 stand for the “union” of cq1 and cq2 ; that is, if cq1 or cq2 is const, then cq12 is const. If E2 is declared to be a mutable member, then the type of E1.E2 is “vq12 T”. If E2 is not declared to be a mutable member, then the type of E1.E2 is “cq12 vq12 T”.

The standard says nothing about E2 becoming a reference even if E1 is. To be explicit, if E1 is a reference type and E2 is not, E1.E2 is not a reference type.

cartographer
  • 495
  • 5
  • 6