equality of references

P

pauldepstein

Suppose

const int& i = 7;
const int& j = 7;


Is i==j true? My compiler says yes. On the other hand, as I
understand it, equality of references means that the addresses being
referenced are identical. Hence, as I understand it, it is quite
possible that the integer 7 is being represented at two different
addresses, and that two different addresses contain a representation
of 7 and so i==j may be false. Is this correct?
I'm not clear on the issue of equality of references.

Thanks for your enlightenment.

Paul Epstein
 
A

Arkaitz Jimenez

const int& i = 7;
const int& j = 7;

Is  i==j   true?

I'd say that as a reference is sometimes considered as "another name"
for a variable.
Comparing 2 references is just comparing those(the referenced ones)
variables, and 2 ints with the same value will always be equal.

Arkaitz
 
A

Andrew Tomazos

Suppose

const int& i = 7;
const int& j = 7;

Is  i==j   true?  My compiler says yes.  On the other hand, as I
understand it, equality of references means that the addresses being
referenced are identical.  Hence, as I understand it, it is quite
possible that the integer 7 is being represented at two different
addresses, and that two different addresses contain a representation
of 7 and so i==j may be false.  Is this correct?
I'm not clear on the issue of equality of references.

Thanks for your enlightenment.

If you want reference equality than do this:

(&i) == (&j)

This will tell you if i and j are really at the same memory location.
-Andrew.
 
J

James Kanze

const int& i = 7;
const int& j = 7;
Is i==j true?

Is 7 == 7? Obviously.
My compiler says yes. On the other hand, as I understand it,
equality of references means that the addresses being
referenced are identical.

Where did you get that. A reference is just an alias for an
object. Once it is initialized, it behaves exactly as if it
were the aliased object.
Hence, as I understand it, it is quite possible that the
integer 7 is being represented at two different addresses, and
that two different addresses contain a representation of 7 and
so i==j may be false. Is this correct?
No.

I'm not clear on the issue of equality of references.

It sounds like you're not clear on the issue of references in
general. Within an expression (any expression), references, as
such, don't exist---a reference is just an lvalue expression
referring to the object used to initialize it.
 
P

pauldepstein

Is 7 == 7?  Obviously.


Where did you get that.  A reference is just an alias for an
object.  Once it is initialized, it behaves exactly as if it
were the aliased object.


It sounds like you're not clear on the issue of references in
general.  Within an expression (any expression), references, as
such, don't exist---a reference is just an lvalue expression
referring to the object used to initialize it.

--
James Kanze (GABI Software)             email:[email protected]
Conseils en informatique orientée objet/
                   Beratung in objektorientierter Datenverarbeitung
9 place Sémard, 78210 St.-Cyr-l'École, France, +33 (0)1 30 23 00 34

I understand more about references than it appears. Suppose int& f
(int, int) is a function that returns a reference to a static local
variable. Then f(x,y) == f(a,b) is always true for all integers
x,y,a,b . I'm not clear exactly why although I've seen several
attempts to explain it.

Paul Epstein

Paul Epstein
 
N

Neelesh

I understand more about references than it appears.  Suppose int& f
(int, int) is a function that returns a reference to a static local
variable.  Then f(x,y) == f(a,b) is always true for all integers
x,y,a,b .  I'm not clear exactly why although I've seen several
attempts to explain it.

Lets see if a small analogy can help. Suppose you have a small sheet
of paper on which you write, say the number 3. Lets imagine two cases
(a) Return by value - This means that you do a photocopy of this sheet
and give the photocopy to your friend. Then you take the original
sheet, and change 3 written on it to, say 33. Then you see the sheet
given to your friend to check if that sheet also got 33 on it. The
answer is - obviously not. This is because what you returned to your
friend is a _copy_, not the original sheet.
(b) Return by reference - This means that you gave the original sheet
to your friend. And then, you change 3 written on the original sheet
to 33. Now you check whether the sheet given to your friend has 3 or
33. Since both of the sheets are actually the same, the sheet given to
your friend also has 33.

In the same way, suppose f(a,b) returns local static variable by
referece and the return value was, say 3. Suppose f(x,y) changes this
3 to say 33. Since what f(a,b) returns is _same_ as (and not the copy
of) the local static varible, when f(x,y) changes to 33, the return
value of f(a,b) also gets changed to 33. As a result, f(a,b) == f(x,y)

Please note that the above analogy is just in order to explain the
current situation.

Hope this helps.
 
S

SG

I understand more about references than it appears.  Suppose int& f
(int, int) is a function that returns a reference to a static local
variable.  Then f(x,y) == f(a,b) is always true for all integers
x,y,a,b .

No. This is not true for this function:

int& f(int a, int b) {
static int p = 3;
static int q = 4;
if (((p^q) & 1)==0) return p;
return q;
}

int main() {
assert( f(0,0) == f(0,1) ); // fails
}
 I'm not clear exactly why although I've seen several
attempts to explain it.

People tried to explain because you don't seem to understand
references. Once a reference has been initialized the reference will
be just another name for the object it refers to. So, if you use the
equality comparison you'll be comparing the values of the aliased
objects and not their addresses. Here are a bunch of other examples:

int main() {
int i = 23;
int j = 23;
int& x = i; // x is an alias for i
int& y = j; // y is an alias for j
assert( x == y); // #1
assert(&x != &y); // #2
assert(&x == &i); // #3
assert(&y == &j); // #4
x = 42;
assert( x != y); // #5
assert(&x != &y); // #2
assert(&x == &i); // #3
assert(&y == &j); // #4
}

1. x and y refer to variables of the same value (23)
2. x and y refer to different variables
3. x refers to i
4. y refers to j
5. x and y refer to variables of different values (23,42)

Cheers!
SG
 
P

pauldepstein

No. This is not true for this function:

  int& f(int a, int b) {
    static int p = 3;
    static int q = 4;
    if (((p^q) & 1)==0) return p;
    return q;
  }

  int main() {
    assert( f(0,0) == f(0,1) ); // fails
  }


People tried to explain because you don't seem to understand
references. Once a reference has been initialized the reference will
be just another name for the object it refers to. So, if you use the
equality comparison you'll be comparing the values of the aliased
objects and not their addresses. Here are a bunch of other examples:

  int main() {
    int i = 23;
    int j = 23;
    int& x = i; // x is an alias for i
    int& y = j; // y is an alias for j
    assert( x ==  y); // #1
    assert(&x != &y); // #2
    assert(&x == &i); // #3
    assert(&y == &j); // #4
    x = 42;
    assert( x !=  y); // #5
    assert(&x != &y); // #2
    assert(&x == &i); // #3
    assert(&y == &j); // #4
  }

1. x and y refer to variables of the same value (23)
2. x and y refer to different variables
3. x refers to i
4. y refers to j
5. x and y refer to variables of different values (23,42)

Cheers!
SG

Ok. I understand the issues now. Thanks for all who helped on this
thread.
There was a small misunderstanding. When I said

"I'm not clear exactly why although I've seen several
attempts to explain it."

What I actually meant was this: "I'm not sure why f(x,y) == f(a,b) in
the reference-to-static case, though from my reading and from browsing
the web, I've seen several attempted explanations of the reference-to-
static scenario."

In particular, the "several attempts" were not by those on this
newsgroup.

Anyway, all is clear now, and thanks again.

Paul Epstein
 
J

James Kanze

[...]
I understand more about references than it appears. Suppose
int& f (int, int) is a function that returns a reference to a
static local variable. Then f(x,y) == f(a,b) is always true
for all integers x,y,a,b.

No it's not.
I'm not clear exactly why although I've seen several attempts
to explain it.

Try it with the following:

int& f( int, int )
{
static int result = 0 ;
++ result ;
return result ;
}
 
J

James Kanze

On May 10, 6:23 pm, (e-mail address removed) wrote:
[...]
I'm not clear on the issue of equality of references.
It sounds like you're not clear on the issue of references
in general. Within an expression (any expression),
references, as such, don't exist---a reference is just an
lvalue expression referring to the object used to
initialize it.
I understand more about references than it appears. Suppose
int& f (int, int) is a function that returns a reference to
a static local variable. Then f(x,y) == f(a,b) is always
true for all integers x,y,a,b.
No it's not.
Try it with the following:
int& f( int, int )
{
static int result = 0 ;
++ result ;
return result ;
}

Sorry for the above; it's wrong. (I was in the middle of doing
something else when I answered it, and didn't really read the
example correctly. My code is really a good example for why
they are always equal.)
 
A

Andrew Tomazos

No. This is not true for this function:

  int& f(int a, int b) {
    static int p = 3;
    static int q = 4;
    if (((p^q) & 1)==0) return p;
    return q;
  }

  int main() {
    assert( f(0,0) == f(0,1) ); // fails
  }

Bug...

- if (((p^q) & 1)==0) return p;
+ if (((a^b) & 1)==0) return p;

-Andrew.
 

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,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top