Testing for References to Identical Objects

I am checking to see if two references are bound to the same object. My instincts tell Me, "Check their addresses. If they match, they are bound to the same." At the same time, I have not found anything in the C++ standard which would support this approach. Am I missing something? Is there wording which backs up My instincts? Is there a standard function to do this?
Nope, you are correct.

The "same object" can only exist in one place.
An "equivalent object" may be a duplicate.

Hence, two references to the same object will have the same address.

1
2
3
4
5
6
7
8
9
10
11
int  x = 12;  // source
int  y = x;   // copy (equivalent object)
int& z = x;  // reference (same object)

cout << boolalpha;

cout << "y == x : " << (y == x) << "\n";
cout << "z == x : " << (z == x) << "\n\n";

cout << "y is x : " << (&y == &x) << "\n";
cout << "z is x : " << (&z == &x) << "\n";
y == x : true
z == x : true

y is x : false
z is x : true

Hope this helps.
Last edited on
Thanks for confirming My instincts but is there specific wording in the standard which makes it so or is there just a presumed convention? I ask because I have some safety-critical code which would depend upon such being behavior standardized.
Alas, I cannot quote the manual, and I'm not going to read it for you.
You can get it here: http://www.open-std.org/jtc1/sc22/wg21/ (the Draft copy is good enough for your purposes).

Maybe cire can find the language you want.
Unless an object is a bit-field or a base class subobject of zero size, the address of that object is the address of the first byte it occupies. Two objects that are not bit-fields may have the same address if one is a subobject of the other, or if at least one is a base class subobject of zero size and they are of different types; otherwise,
they shall have distinct addresses.

Footnote: Under the “as-if” rule an implementation is allowed to store two objects at the same machine address or not store an object at all if the program cannot observe the difference - IS 1.8 The C++ Object Model


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <memory>
#include <type_traits>

template < typename T, typename U > bool have_same_address( const T& a, const U& b ) 
{ return reinterpret_cast<const char*>( std::addressof(a) ) == reinterpret_cast<const char*>( std::addressof(b) ) ; }

template < typename T, typename U > bool is_same_object( const T& a, const U& b ) 
{ return std::is_same<T,U>::value && have_same_address(a,b) ; }

int main()
{
    struct A { int v = 10 ; } ;
    struct B { A a ; double d = 0 ; const B* operator&() const { return nullptr ; } } ;
    
    B one, two ;
    std::cout << std::boolalpha << ( &one == &two ) << '\n' // true
              << have_same_address(one,two) << '\n'  // false
              << have_same_address( one, one.a ) << '\n' // true
              << is_same_object( one, one.a ) << '\n' ; // false
}

http://coliru.stacked-crooked.com/a/caec7f6f632d31a1
Registered users can post here. Sign in or register to post.