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

Discussion in 'C++' started by jaabelloc@yahoo.com, Jul 2, 2013.

  1. Guest

    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&.
    , Jul 2, 2013
    #1
    1. Advertising

  2. On 7/2/2013 2:43 PM, wrote:
    > 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
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 2, 2013
    #2
    1. Advertising

  3. Guest

    On Tuesday, July 2, 2013 3:43:15 PM UTC-3, wrote:
    > 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.
    , Jul 2, 2013
    #3
  4. On 7/2/2013 3:22 PM, wrote:
    > On Tuesday, July 2, 2013 3:43:15 PM UTC-3, wrote:
    >> 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,

    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
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 2, 2013
    #4
  5. Öö Tiib Guest

    On Tuesday, 2 July 2013 22:22:49 UTC+3, wrote:
    > On Tuesday, July 2, 2013 3:43:15 PM UTC-3, wrote:
    > > 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,
    > 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.
    Öö Tiib, Jul 2, 2013
    #5
  6. Belloc Guest

    On Tuesday, July 2, 2013 5:20:52 PM UTC-3, Victor Bazarov wrote:
    >
    > 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.
    Belloc, Jul 2, 2013
    #6
  7. Belloc Guest

    On Tuesday, July 2, 2013 5:23:53 PM UTC-3, Öö Tiib wrote:
    >
    > 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;
    Belloc, Jul 2, 2013
    #7
  8. On 7/2/2013 4:36 PM, Belloc wrote:
    > On Tuesday, July 2, 2013 5:20:52 PM UTC-3, Victor Bazarov wrote:
    >>
    >> 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.


    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
    --
    I do not respond to top-posted replies, please don't ask
    Victor Bazarov, Jul 2, 2013
    #8
  9. Öö Tiib Guest

    On Tuesday, 2 July 2013 23:42:43 UTC+3, Belloc wrote:
    > On Tuesday, July 2, 2013 5:23:53 PM UTC-3, Öö Tiib wrote:
    > > 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;


    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.
    Öö Tiib, Jul 2, 2013
    #9
  10. Belloc Guest

    On Tuesday, July 2, 2013 6:39:15 PM UTC-3, Victor Bazarov wrote:
    >
    > 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.
    Belloc, Jul 2, 2013
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jeff Smythe
    Replies:
    3
    Views:
    1,217
    Jeff Smythe
    Jan 2, 2004
  2. bingfeng
    Replies:
    12
    Views:
    481
  3. Trevor Fritz
    Replies:
    2
    Views:
    158
    Feroze [MSFT]
    May 11, 2004
  4. pantagruel
    Replies:
    0
    Views:
    219
    pantagruel
    Feb 17, 2006
  5. revek
    Replies:
    15
    Views:
    151
    Thomas 'PointedEars' Lahn
    Feb 2, 2009
Loading...

Share This Page