Using 8.5.3p4 in C++11 Standard, how do I establish that T isreference-compatible to a T&?

J

jaabelloc

Given

T w;
T& t = w;

how do I establish, using 8.5.3p4 and p5, that r binds to t below, i.e., that T is reference-compatible with T&?

T& r = t;

Observe that T is not the same type as T&, neither a base class of T&, and so according to 8.5.3p4 we can't say that T is reference-related to T&.
 
V

Victor Bazarov

Given

T w;
T& t = w;

how do I establish, using 8.5.3p4 and p5, that r binds to t below,
i.e., that T is reference-compatible with T&?
T& r = t;

Observe that T is not the same type as T&, neither a base class of
T&,
and so according to 8.5.3p4 we can't say that T is reference-related to T&.

Could it be that you think that T& is a type in a variable declaration?
It's not. T is a type and '&' is the syntax to declare the variable a
reference. IOW, you can write

T w, &t = w;

so both 'w' and '&t' are of the same type T. Would that help?

V
 
J

jaabelloc

Given



T w;

T& t = w;



how do I establish, using 8.5.3p4 and p5, that r binds to t below, i.e., that T is reference-compatible with T&?



T& r = t;



Observe that T is not the same type as T&, neither a base class of T&, and so according to 8.5.3p4 we can't say that T is reference-related to T&.

I'm looking specifically at the declaration T& r = t; which is legal, butI can't see how 8.5.3p4 could help me to assert that 'r' is reference-related to 't'. I'm considering in this case: T1 = T and T2 = T&. But T1 isnot equal to T2, nor T1 is a base class of T2.
 
V

Victor Bazarov

I'm looking specifically at the declaration T& r = t; which is
legal,
but I can't see how 8.5.3p4 could help me to assert that 'r' is
reference-related to 't'. I'm considering in this case: T1 = T and T2 =
T&. But T1 is not equal to T2, nor T1 is a base class of T2.
I honestly don't understand (yet) with what part of paragraph 4 of that
subclause you have a problem. It explains (establishes) the
relationship between two classes T1 and T2, and mentions reference
binding. In your case T is the same as T, and as such they are
reference-related. Read the next paragraph. A reference to type "cv1
T1" is initialized by an expression of type "cv2 T2"... In your case T1
is T and T2 is T. Substitute those and get: a *reference to type T* is
initialized by an *expression of type T*. So, the '&' is not part of
the 'cv1 T1' or 'cv2 T2', AFA reference initialization is concerned.

V
 
Ö

Öö Tiib

I'm looking specifically at the declaration T& r = t; which is legal,
but I can't see how 8.5.3p4 could help me to assert that 'r' is
reference-related to 't'.

The implementations handle references more or less like pointers (like
immutable pointers). So your 'r' is reference to 'w' and not to
't'. Same as here:
int w;
int* const t = &w;
int* const r = t;

I'm considering in this case: T1 = T and T2 = T&. But T1 is not equal
to T2, nor T1 is a base class of T2.

On that case T1 is not even reference-related to T2 and so it can't be
reference-compatible and so reference binding between such types
is invalid.

legaleze art under question:
Given types “cv1 T1” and “cv2 T2,” “cv1 T1” is reference-related
to “cv2 T2” if T1 is the same type as T2, or T1 is a base class of
T2. “cv1 T1” is reference-compatible with “cv2 T2” if T1 is
reference-related to T2 and cv1 is the same cv-qualification as,
or greater cv-qualification than, cv2. In all cases where the
reference-related or reference-compatible relationship of two
types is used to establish the validity of a reference binding,
and T1 is a base class of T2, a program that necessitates such a
binding is ill-formed if T1 is an inaccessible (Clause 11) or
ambiguous (10.2) base class of T2.
 
B

Belloc

I honestly don't understand (yet) with what part of paragraph 4 of that

subclause you have a problem. It explains (establishes) the

relationship between two classes T1 and T2, and mentions reference

binding. In your case T is the same as T, and as such they are

reference-related. Read the next paragraph. A reference to type "cv1

T1" is initialized by an expression of type "cv2 T2"... In your case T1

is T and T2 is T. Substitute those and get: a *reference to type T* is

initialized by an *expression of type T*. So, the '&' is not part of

the 'cv1 T1' or 'cv2 T2', AFA reference initialization is concerned.

I dispute the fact that T2 is a T in the declaration "T& r = t;". For me T2 is a T&. If the declaration was something like "T& r = w;" with 'w' being a T, then yes, T2 would be a T. Otherwise how would differentiate the types between these these two declarations.
 
B

Belloc

On that case T1 is not even reference-related to T2 and so it can't be

reference-compatible and so reference binding between such types

is invalid.

This is exactly what I'm trying to say, which is an absurd as the code below is legal:

T w;
T& t = w;
T& r = t;
 
V

Victor Bazarov

I dispute the fact that T2 is a T in the declaration "T& r = t;". For
me T2 is a T&. If the declaration was something like "T& r = w;" with
'w' being a T, then yes, T2 would be a T. Otherwise how would
differentiate the types between these these two declarations.

Oh, OK, I think I understand now. Sorry it took me so long. You are
talking about the right-hand side. The expression 't' (with which 'r'
is initialized) seems to contain the &, from your point of view. Well,
it does not. You need to turn to the chapter 5, expressions, paragraph
5 ([expr]/5).

V
 
Ö

Öö Tiib

This is exactly what I'm trying to say, which is an absurd as the code
below is legal:

T w;
T& t = w;
T& r = t;

There are no such thing like "references to references" in C++ (§ 8.3.2p5)
so something of what you try to say is indeed absurd.

Maybe eyeball that: ($5p5)
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. The expression
designates the object or function denoted by the reference, and the
expression is an lvalue or an xvalue, depending on the expression.


The expression 't' above has type 'T' (and it is not 'T&') prior of any
further absurd analysis you might want to apply.
 
B

Belloc

Oh, OK, I think I understand now. Sorry it took me so long. You are

talking about the right-hand side. The expression 't' (with which 'r'

is initialized) seems to contain the &, from your point of view. Well,

it does not. You need to turn to the chapter 5, expressions, paragraph

5 ([expr]/5).

It seems like you got it. I knew about this clause, but I thought it didn't apply to expressions within a declaration as "T& r = t;", but it makes sense that 5p5 should apply to 't', as it defines the reference r. Many thanks.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,578
Members
45,052
Latest member
LucyCarper

Latest Threads

Top