1

std::unique_ptr is for exclusive ownership.

std::shared_ptr is for shared ownership.

So it looks like a raw pointer is left just the job of being a non-owning pointer, at least in good code.

But if I see a raw pointer somewhere, I actually don't know if it is truly just observing the pointee, or it is used somewhere to delete it or to create a new object.

Is there anything in the C++ language, or an accepted idiom, to model a non-owning view on some resource?

Enlico
  • 23,259
  • 6
  • 48
  • 102
  • there is `gsl::owner`. If used consistently, all raw pointers are non-owning – 463035818_is_not_an_ai Mar 27 '23 at 09:21
  • 1
    What about [`std::weak_ptr`](https://en.cppreference.com/w/cpp/memory/weak_ptr)? – Aziuth Mar 27 '23 at 09:25
  • 1
    @Aziuth, the parenthesis in the title of the question rule it out. – Enlico Mar 27 '23 at 09:26
  • The std::weak_ptr is meant as non-owning pointer. – Öö Tiib Mar 27 '23 at 09:26
  • 2
    @ÖöTiib, it needs the object to be already managed by a `std::shared_ptr`, doesn't it? – Enlico Mar 27 '23 at 09:27
  • In your desired model, which part of the program would then actually do the ownership/memory management? That is, which problems do you have in particular for which `weak_ptr` or a reference don't work? Actually asking out of curiosity. – Aziuth Mar 27 '23 at 09:30
  • Yes, but by what it *is* managed? The raw pointer is worst because it is anything: array, iterator, one-past-last, dynamic object, reference. – Öö Tiib Mar 27 '23 at 09:30
  • You could use `template observer_ptr = T*` (or use a class with similar interface as smart pointers) to explicitly imply that the raw pointer is non-owning. – ALX23z Mar 27 '23 at 09:53
  • @463035818_is_not_a_number, if a pointer is owning, then I'd make it a `std::unique_ptr`. As in, _if `std::unique_ptr`/`shared_ptr` were used consistently, all raw pointers would be non-owning. – Enlico Mar 27 '23 at 10:12
  • @Enlico yes but I understood its legacy. `std::owner` is much less intrusive than smart pointers. Its just a tagged raw pointer. You can use it on raw pointers managed by other code without the need to change that other code. – 463035818_is_not_an_ai Mar 27 '23 at 10:14
  • well ok, that last comment doesnt make sense :D. I don't delete it just for reference how its not a solution – 463035818_is_not_an_ai Mar 27 '23 at 10:17
  • 1
    If the raw pointer is on function boundaries (in function arguments or the return type), it's most probably time-bomb; A bug will show up sooner or later. The main job for raw pointers is to interface C API libraries, and language inter-operablity; that's why in good C++ code, raw pointer should only appear on function boundaries. Some complex data structures might still need raw pointers too, but those are corner cases for crazy library programmers. – Red.Wave Mar 27 '23 at 10:39

1 Answers1

1

There is std::experimental::observer_ptr which is a pointer without ownership.

Jarod42
  • 203,559
  • 14
  • 181
  • 302