John
John

Reputation: 3524

How to comprehend "std::relational operators (shared_ptr) compares directly the stored pointers"

As per the documentation(http://www.cplusplus.com/reference/memory/shared_ptr/operators/), which says: [emphasis mine]

The comparison compares directly the stored pointers (i.e., the value the objects dereference to, and not their owned pointer (i.e., the managed objects that are deleted on destruction), which may not be the same in alias shared_ptr objects (alias-constructed objects and their copies).

I can understand the example code in the documentation aforedmentioned to some degree.But I could not comprehend the quotes above.

Could somebody make it clear by giving some simple examlples and explain when using this operator makes sense?

I have been confused for a long time. I would be very grateful to have some help with this question.

Here is the example code which i can understand to some degree:

// shared_ptr relational operators
#include <iostream>
#include <memory>

int main () {
  std::shared_ptr<int> a,b,c,d;

  a = std::make_shared<int> (10);
  b = std::make_shared<int> (10);
  c = b;

  std::cout << "comparisons:\n" << std::boolalpha;

  std::cout << "a == b: " << (a==b) << '\n';
  std::cout << "b == c: " << (b==c) << '\n';
  std::cout << "c == d: " << (c==d) << '\n';

  std::cout << "a != nullptr: " << (a!=nullptr) << '\n';
  std::cout << "b != nullptr: " << (b!=nullptr) << '\n';
  std::cout << "c != nullptr: " << (c!=nullptr) << '\n';
  std::cout << "d != nullptr: " << (d!=nullptr) << '\n';

  return 0;
}

Upvotes: 0

Views: 51

Answers (2)

John
John

Reputation: 3524

I post an answer which has been deleted by its creator. I think it's helpful to comprehend the example code in the question. Hope it helps to the one who see this question.

A shared_ptr points to an object that it manages. It is the one you get by shared_ptr::get. Expression a == b on shared_ptrs a and b tests whether a and b point to the same object, i.e. whether a.get() == b.get(). It returns true in this case. If a and b point to different objects, even if these two objects compared equal, the result is false.

Let's have a look on the sample code:

a = std::make_shared<int> (10);
b = std::make_shared<int> (10);

This creates two shared pointers pointing to separate integer objects. Even if these two integer objects have the same value 10, they are still different objects. So a.get() will be different from b.get(), and, consequently a==b returns false.

However, once you assign

c = b the two shared_ptrs b and c will point to the same object, such that b.get() returns a pointer to the same object as c.get(). Hence, c == b returns true.

That a and b point to different objects, whereas b and c share the same object, can be illustrated as follows:

a = std::make_shared<int> (10);
b = std::make_shared<int> (10);
c = b;

*b = 30;  // change the value of the managed object.

std::cout << *a << std::endl; // still prints 10;
std::cout << *b << std::endl; // prints 30;
std::cout << *c << std::endl; // prints 30, as it points to the same integral object as b
Hope it got clearer.

Upvotes: 1

j6t
j6t

Reputation: 13572

Let's say, you have this struct:

struct Foo { int a, b; };

Then you can generate aliased shared pointers like this:

void f()
{
    // create
    shared_ptr<Foo> orig(new Foo{1, 2});
    // aliased pointer 1, points into object held by orig
    shared_ptr<int> pa(orig, &orig->a);
    // aliased pointer 2, ditto
    shared_ptr<int> pb(orig, &orig->b);

    assert(pa != pb);    // passes
}

Even though pa and pb share ownership of the same object (which they happen to share with orig as well), they carry different pointers, just like you would expect.

Upvotes: 3

Related Questions