How can I compare two std::reference_wrapper
s by the references they hold? I want to see if the references of two std::reference_wrapper
s are equal or not.
Edit: Sorry from confusion. I meant how to get the addresses of referents and compare them.
An std::reference_wrapper is a copyable and assignable object that emulates a reference. Contrary to its name, it does not wrap a reference. It works by encapsulating a pointer ( T*) and by implicitly converting to a reference ( T& ). It cannot be default constructed or initialized with a temporary; therefore, it cannot be null or invalid:
Also, we cannot get the address of a reference itself because a reference is not an object ( a region of storage) according to the C++ standard. Declaring a reference to a reference, an array of references, and a pointer to a reference is forbidden in C++.
2. Motivation C++ references are favored over pointers wherever possible because they cannot be null, appropriately express by-reference intent, and have better readability because of no dereferencing (*, ->) jumble. However, a reference is a stubborn alias of an object.
Reference as a class member Having a reference class member poses problems, such that, it makes the class non-assignable and practically immovable: The usual practice is to avoid references as class members and use pointers instead. 3.5. Passing a function object by reference
The get()
member function returns a reference to the element referred to. You can then take the addresses of the referents directly.
std::addressof(r1.get()) == std::addressof(r2.get())
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With