Hacker News new | ask | show | jobs
by gumby 1414 days ago
> C++ also pays a price for insisting not only that objects have addresses, but those addresses are distinct.

With only three exceptions I can't think of a case where by default one would not want that. In the case you describe you would want all those objects to have unique addresses. If you wanted to have them overlap you should go to the effort to happen the way you want it to -- how could the compiler guess on its own?

The exceptions BTW are union elements, the first class/struct element, and base object addresses (in `class foo : bar ...` the when you make a foo, the address of its bar is the same as the address of the foo itself).

1 comments

> In the case you describe you would want all those objects to have unique addresses.

I certainly don't, if you want unique addresses for indistiguishable objects I guess C++ is the perfect language for you but, what are you expecting to do with these addresses?

You made an object rather than using an existing one. How is the compiler supposed to know that you wanted an existing object?
I don't want an existing object? You seem to be having a lot of trouble with this very basic idea. Objects do not necessarily need distinct addresses. "But that's how C++ works" is just a fact about how C++ works, it has no larger significance, and where it conflicts with the sensible way to do things it's actually a defect.
How would you define object identity if different objects can have the same value and the same address?

To my mind, an object is a value with a unique identity. How else would you define it?

And if you want a billion empty values, then yes, those could all be implemented by the same object - it's pretty easy to implement, even though it would be ncie for a compiler to do it automatically (like how Java normally gives you the same Integer object if you box the same int value in two different places, even though it will give you a different Integer of the same value if you explicitly ask for it with new).

> How would you define object identity

    fn main() {
         let vs = [(), (), (), (), ()];
         for v in vs {
             println!("{:?}", v);
         }
    }
This loop will five times print "()", because `v` will iterate through all five elements of `vs`. Are this values are identical or they aren't? I don't know, it doesn't matter, isn't it? But I think of them as of different values: they are different members of `vs`.
They are the same value, but with different identities. vs[0], vs[1] etc. are different objects, but they are all initialized with the same value. The difference is kind of irrelevant for a constant object like an empty tuple.

But imagine the following program:

  fn main() {
    let mut vs = [(1,), (1,), (1,), (1,), (1,)];
    vs[0].0=2;
    for v in vs {
      println!("{:?}", v);
    }
  }
Here we can see that identity is in fact important: `vs[0].0 = 2` only modifies one of the objects, even if all of them initially had the same value.

By the way, note that your example should be completely equivalent to the following C++ program:

  int main() {
     using namespace std;
     auto vs = vector<tuple<>>{tuple<>(), tuple<>(), tuple<>(), tuple<>()};
     for (auto v : vs) {
       cout << v; //imagine C++ actually had an implemntation of operator<< for tuples...
     }
     return 0;
  }
What are you expecting to do with 4 billion empty tuples?