Re:Deprecating Raw Pointers in C++20
by evincarofautumn on Sunday April 01, 2018 @07:39AM
(#16727782)
In all seriousness, reference_wrapper lets you:
1. Have a container of non-null references. As the example in your cppreference link shows, that’s convenient for efficiently providing multiple “views” of the same values.
2. Pass a value with reference semantics to a function that takes an argument by value. Mainly useful to avoid copying or reuse a function object.
3. Explicitly say “this is a reference to something I don’t own”, complementing unique_ptr’s “reference to something I own exclusively”, shared_ptr’s “reference to something I share ownership of”, and raw pointers’ “reference to something I may or may not own”.
1. Have a container of non-null references. As the example in your cppreference link shows, that’s convenient for efficiently providing multiple “views” of the same values.
2. Pass a value with reference semantics to a function that takes an argument by value. Mainly useful to avoid copying or reuse a function object.
3. Explicitly say “this is a reference to something I don’t own”, complementing unique_ptr’s “reference to something I own exclusively”, shared_ptr’s “reference to something I share ownership of”, and raw pointers’ “reference to something I may or may not own”.