C++ Logo


Advanced search

equality compare reference_wrapper

From: Bernard <bpwlodarski_at_[hidden]>
Date: Thu, 25 Mar 2021 21:31:47 -0400
// Microsoft Preview - Features from the Latest C++ Working Draft
// I am trying to equality compare two reference_wrapper objects. The
compiler generates an error if the contained type is a class so I wrote an
override. However as you can see below the compiler still generates an
error. Kindly advise Thank You

#include <functional>

using namespace std;

class cfoobar {
public: bool operator == (const cfoobar& rhs) const { return true; };

template<typename valueType>
bool operator == (const reference_wrapper<valueType>& lhs, const
reference_wrapper<valueType>& rhs) { return lhs.get == rhs.get; }

int main()
int i = 0;
int j = 0;
reference_wrapper<int> rwi = i;
reference_wrapper<int> rwj = j;
// line below compiles w/o error
rwi == rwj;

cfoobar a;
cfoobar b;
reference_wrapper<cfoobar> rwa = a;
reference_wrapper<cfoobar> rwb = b;
// line below compiles w/ error as shown
// error C2676 : binary '==' : 'std::reference_wrapper<cfoobar>' does not
define this operator or
// a conversion to a type acceptable to the predefined operator
rwa == rwb;

Received on 2021-03-25 20:32:27