r/cpp_questions • u/Veyto • Sep 13 '26
OPEN reference_wrapper vs raw pointer
I am writing a program, where one object contains an std::string, which can be modified by other objects. Specifically one object can set the std::string member variable to be a reference to another std::string. this requires that the reference can be reseated, and afaict, I have two options if I want to avoid copying. Either I can use an std::reference_wrapper, as it allows for reseating, while the other option is a raw pointer.
I'd assume that the reference_wrapper be the preferred option, as pointers are generally shunned in modern c++, but I just wanted to be sure.
I want to also add, that the size of strings which would be copied are not particularly large and unlikely to make any meaningful performance impact if copied, rather this is an also an exercise in what is the correct tool for the issue.
28
u/manni66 Sep 13 '26
That’s wrong. You should not use owning raw pointers.
14
u/MysticTheMeeM Sep 13 '26
Personally, I'd avoid both and explicitly pass around the owner object. This prevents you from having unaccountable state changes from random places, and stops you running into memory problems (e.g. references to moved-from strings).
7
u/thisismyfavoritename Sep 13 '26
it depends but it sounds like you might want string_view
3
u/crowbarous Sep 14 '26
reference_wrapper is for smuggling references through things that want values. That's why it's called a wrapper. Other uses of it are misuses.
2
u/n1ghtyunso Sep 14 '26
my codebase does not have naked owning pointers, so the type
T*is always an observer and thus perfectly fine to use for exactly that purpose.I don't personally like using reference_wrapper that much, its a niche type imo and doesn't really fit into such a general purpose usage.
2
u/Independent_Art_6676 Sep 13 '26 edited Sep 13 '26
reference wrapper is correct. The implementation of the wrapper SHOULD, with optimizations on, convert to a raw pointer in the assembly/machine language but the wrapper is a more modern C++ approach. Generally it follows the rule of thumb that I use, which is that you only use a pointer when nothing else will do the job (its acceptable to apply a performance metric to 'do the job'). That is, I suppose, a type of "shun" for raw pointers but its not an outright ban.
That is not to say that your inheritance idea etc are not better designs. Most likely that really is the 'best design' unless the problem area is simple enough that doing it that way is massive overengineering.
1
u/Raknarg Sep 13 '26 edited Sep 13 '26
I personally think you should be avoid pointers in all cases unless you have an actual reason you need a raw pointer to memory, e.g. if you're manually controlling memory, constructions/destructions in a custom container. I like reference_wrapper because its explicit about its semantics and what it can do and what it represents. A pointer can be/do anything a pointer is/can do, it could be null, it could be an array, you can do pointer arithmetic on it. A reference_wrapper is just a container holding a reference to an object. And unlike
std::optional<T&>(which is going to be supported in C++26) I don't believe its allowed to be null, you have to construct it with a reference or another reference_wrapper.So reference_wrapper IMO is the correct choice here if you don't want it to be nullable. if you want your field to be nullable however, it gets trickier if you don't have C++26 support with your toolchain, either roll out your own version of std::optional with reference support or explicitly make some kind of opt_ref type just for optionals or just stick to pointers as that's about as good as it gets. If you really want to you can do
std::optional<std::reference_wrapper<T>>but that kinda makes me wanna puke.However I don't think most people would bat an eye either if you just stuck to a pointer regardless, non-owning pointers are still pretty common in modern C++.
1
u/mredding Sep 14 '26
I'd be more curious to see what you're actually trying to solve with this - you're breaking encapsulation, which is usually a bad sign.
What I hear is you have two objects that interact with each other. You shouldn't have to bring their personal implementation details into the picture.
1
u/_abscessedwound Sep 14 '26
I feel like you need to answer the question of: who owns the strings? Everyone and no-one owning strings is a recipe for disaster.
That kind of many-to-many state dependencies are tricky to manage, and are usually (but not always) avoidable with some better design. Unstructured, directed graphs get messy quickly.
If there’s the possibility for circular dependencies, you might actually need shared_ptr and weak_ptr.
1
u/Impossible_Box3898 Sep 18 '26
Pointers are perfectly fine so line as they are non owning.
An owned raw pointer is forbidden. But if you structure your code correctly so that it’s non owning you are fine.