Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Comparing addresses of reference_wrappers

How can I compare two std::reference_wrappers by the references they hold? I want to see if the references of two std::reference_wrappers are equal or not.

Edit: Sorry from confusion. I meant how to get the addresses of referents and compare them.

like image 596
Shibli Avatar asked Jun 06 '14 00:06

Shibli


People also ask

What is a reference wrapper?

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:

Can we get the address of a reference in C++?

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++.

Why do we use references in C++ instead of pointers?

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.

Can a class have a reference as a class member?

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


1 Answers

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())
like image 186
Brian Bi Avatar answered Sep 18 '22 23:09

Brian Bi