4 ms·
Passing things by reference is safe. Any other use is mostly just pointless; it what reference types are for. If you are doing something else with them, it easy
by ncmncm 7y ago
Passing things by reference is safe. Any other use is mostly just pointless; it what reference types are for. If you are doing something else with them, it easy to stop.
- roca 7y agoPassing parameters by reference is certainly not always safe. You can, for example, pass a reference to a deref'ed unique_ptr into a function call, while the function call does something that clears the unique_ptr. If you want to avoid objects containing references (or raw pointers), then you have to avoid using lots of standard library stuff --- `string_view`, C++20 `span`, or even STL iterators.
- ncmncm 7y agoNow you are talking about pointers.
- roca 7y agoThere isn't much difference between references and pointers. That is the problem.
- ncmncm 7y agoA reference only ever points at one thing, from birth to death, and it always points at something.
- roca 7y agoNo, it is entirely possible to delete the underlying object so a reference is left dangling. See https://github.com/isocpp/CppCoreGuidelines/issues/1038 https://github.com/isocpp/CppCoreGuidelines/issues/1038 for example.