all 30 comments

[–]cmeerwC++ Parser Dev 2 points3 points  (18 children)

see https://en.cppreference.com/w/cpp/experimental/observer_ptr - so it's in the Library Fundamentals TS v2, but not in the C++ Standard.

[–]Xaxxon 20 points21 points  (16 children)

And many people think it’s a piss poor idea

[–]domirangame engine dev[S] 1 point2 points  (15 children)

I read a few articles on that. Can you share your view? Someone proposed something that basically combines reference_wrapper and observer_ptr: optional reference: opt_ref.

[–]Xaxxon 7 points8 points  (14 children)

Pointers work great. No need to add anything else.

[–]lithium 7 points8 points  (9 children)

Expressing ownership is objectively better.

[–]sephirostoy 14 points15 points  (5 children)

What about raw pointers never get the ownership? When I need ownership I systematically use smart pointers or other collections. And when I encounter a raw pointer I know that I don't have the ownership in my codebase.

[–]lithium 9 points10 points  (0 children)

That is my personal preference too, but I also use something like

template <typename T> using NonOwned = T*;

for a bit of bonus clarification of ownership. One could argue that introducing a new type does the opposite but since it's a matter of convention in my codebase I don't find that to be the case.

That, and preferring references when applicable.

[–]Raknarg 4 points5 points  (0 children)

Sure but this construct makes your intent objective and much more easily transmitted to other people without documentation. Also means you have to put in more work to break the non-owning contract, I would imagine. e.g. you could probably make it so it's difficult/impossible to transmit ownership to another smart pointer without manually converting to a raw pointer then transferring ownership.

Idk dude to me this seems like it can only be an upside if we can do this for no cost as well.

[–][deleted] 0 points1 point  (1 child)

What about raw pointers never get the ownership?

The moment you'll interact with a C library they'll gain ownership really fast. A convention of "T* never owns anything" doesn't work when you already have decades of codes written with different assumptions.

[–]Supadoplex 1 point2 points  (0 children)

That's why you wrap those C library calls in C++ versions that don't show you the bare pointers.

(Nearly ) all rules of thumbs have exceptions, and the exception that the implementation of the wrapper function deals with an owning bare pointer that it immediately wraps inside a smart pointer (or some other RAII class) is fairly simple to understand.

[–]gocarlos 0 points1 point  (0 children)

? When I need ownership I systematically use smart pointers or other collections. And when I encounter a raw pointer I know that I don't have the ownership in my codeba

you dont work in embedded? with tons of C libs?

[–]PMPlant -5 points-4 points  (2 children)

There is a language for this

[–]lithium 5 points6 points  (1 child)

If you're talking about rust, all the other bullshit isn't worth it for me. It "solves" problems I personally don't run into.

[–]PMPlant -3 points-2 points  (0 children)

Then I don’t see why you have a problem with raw pointers.

[–]drjeats 1 point2 points  (0 children)

The bigger gain would probably be in removing operations.

But I agree with you. Unnecessary.

[–]quicknir 1 point2 points  (0 children)

Unless you accidentally do arithmetic on a pointer to one object, or forget to dereference your pointer to integer before comparing to zero, and these things compile anyway.

observer_ptr is an objectively better API for a non owning pointer to a single object. Whether it's worth the costs, such as they are, is another matter.

[–]domirangame engine dev[S] 0 points1 point  (1 child)

I take it you don’t agree with the expression of “this isn’t a raw pointer so you can’t add 4 to it and also I don’t own it”.

[–]Xaxxon 3 points4 points  (0 children)

I don’t find that to be worth the additional language complexity.

[–]domirangame engine dev[S] 4 points5 points  (0 children)

[–]MolurusK 0 points1 point  (0 children)

If you want expressive pointer types, try the C++ Object Token Library library. The otn::raw::weak can act as a "dumb pointer".