Error compile "no match for operator==" [duplicate] - c++

This question already has an answer here:
Closed 10 years ago.
Possible Duplicate:
no matching function error using template template parameters in a function
Good mornig I don't understand because I obtain compile error by implementing as global function the operator== (in class myList::iterator)
If I realize the operator== as member function the code compile.
The error is
error: no match for operator== in it == it1
note: candidate is:
note: tempalte<class T> bool operator==(const typename myList<T>::iterator&,const typename myList<T>::iterator&)
The code is:
#include <iostream>
#include <cstdlib>
#include <iterator>
#include <typeinfo>
template <class T>
class myList;
template <class T>
bool operator==(const typename myList<T>::iterator& lhs,const typename myList<T>::iterator& rhs);
template <class T>
bool operator!=(const typename myList<T>::iterator& lhs,const typename myList<T>::iterator& rhs);
template <class T>
class myList
{
private:
class myInfo;
public:
//CTR DEFAULT
myList():_pInfo(NULL)
{}
myList(T value):_pInfo(new myInfo(value))
{}
// class iterator;
// friend class iterator;
class iterator{
public:
//creo gli iteratori
iterator():_pMyInfoIt(NULL)
{}
iterator(myInfo* p):_pMyInfoIt(p)
{}
iterator(const iterator& it):_pMyInfoIt(it._pMyInfoIt)
{}
iterator& operator=(const iterator& it)
{
_pMyInfoIt = it._pMyInfoIt;
return *this;
}
//creo funzioni che lavorano sugli iteratori
/*
bool operator==(const iterator& rhs)
{
return _pMyInfoIt == rhs._pMyInfoIt;
}
*/
friend bool operator== <T>(const typename myList::iterator& lhs,const typename myList::iterator& rhs);
friend bool operator!= <T>(const typename myList<T>::iterator& lhs,const typename myList<T>::iterator& rhs);
private:
myInfo* _pMyInfoIt;
};
myList::iterator begin()
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
return iterator(_pInfo);
}
private:
class myInfo
{
public:
myInfo(const T& data):_data(data),_pMyInfo(NULL)
{}
private:
T _data;
myInfo* _pMyInfo;
};
myInfo* _pInfo;
};
template <class T>
bool operator==(const typename myList<T>::iterator& lhs,const typename myList<T>::iterator& rhs)
{
std::cout << __PRETTY_FUNCTION__ << std::endl;
return lhs._pMyInfoIt == rhs._pMyInfoIt;
}
template <class T>
bool operator!=(const typename myList<T>::iterator& lhs,const typename myList<T>::iterator& rhs)
{
return !(lhs == rhs);
}
int main(int argc,char** argv){
myList<int> test;
myList<int>::iterator it = test.begin();
myList<int>::iterator it1 = test.begin();
std::cout << typeid(it1).name() << std::endl;
if(it == it1)
std::cout << "EQUAL" << std::endl;
return EXIT_SUCCESS;
}
thanks in advance

Compiler can not deduce T. Change to:
template <class IT, class=typename IT::iterator_category>
bool operator==(const IT& lhs, const IT& rhs)
2nd template parameter does simple SFINAE, so that only iterators will match.

Define the friend functions within the class body:
friend bool operator== <>(const typename myList<T>::iterator& lhs, const typename myList<T>::iterator& rhs)
{
std::cout << __LINE__ << std::endl;
return lhs._pMyInfoIt == rhs._pMyInfoIt;
}
friend bool operator!= <>(const typename myList<T>::iterator& lhs, const typename myList<T>::iterator& rhs)
{
return !(lhs == rhs);
}
It should work.

As pimanych says, declare the body of the friend function inside the class body:
friend bool operator==(const iterator& lhs, const iterator& rhs)
{
return lhs._pMyInfoIt == rhs._pMyInfoIt;
}
friend bool operator!=(const iterator& lhs, const iterator& rhs)
{
return !(lhs == rhs);
}
Note that these aren't really template functions, but are only declared when myList<T> (and myList<T>::iterator) is instantiated. You can check by attempting to compare a myList<int>::iterator and myList<float>::iterator - you should get a compiler error.
Look up the Barton-Nackman trick for more details.

Related

C++ cast template type

Consider the following code snippet :
[[nodiscard]] bool operator==(const BasicIterator<const Type>& rhs) const noexcept {
if( this == &rhs ) {
return true;
}
return node_ == rhs.node_;
}
[[nodiscard]] bool operator==(const BasicIterator<Type>& rhs) const noexcept {
// how to call existing operator== implementation ??
//return operator==( rhs );
}
How should I use the same implementation for both operator==? Is it possible to call operator== <const Type> version from operator== <Type>?
Is there any cast for template types in this case?
Is Type a template? If not, maybe make it a template?
template <typename T>
[[nodiscard]] bool operator==(const BasicIterator<T>& rhs) const noexcept {
if( this == &rhs ) {
return true;
}
return node_ == rhs.node_;
}
If you do not want to expose this template to users, hide it somewhere and use in implementation:
private:
template <typename T>
[[nodiscard]] bool operator==(const BasicIterator<T>& lhs, const BasicIterator<T>& rhs) const noexcept {
if( &lhs== &rhs ) {
return true;
}
return lhs.node_ == rhs.node_;
}
public:
[[nodiscard]] bool operator==(const BasicIterator<const Type>& rhs) const noexcept {
return operator==<const Type>(*this, rhs);
}
[[nodiscard]] bool operator==(const BasicIterator<Type>& rhs) const noexcept {
return operator==<Type>(*this, rhs);
}

Random access iterator: What am I missing?

I have a simple array-based container and I want to make an iterator for it. My goal is to make std::sort work so I'm using random access iterator.
Iterator Class:
class MyIterator: public std::iterator<std::random_access_iterator_tag, T> {
friend class ArraySequence;
private:
T* pos;
MyIterator(T* pos);
public:
MyIterator(const MyIterator &it);
~MyIterator();
public:
typename MyIterator::reference operator*() const;
typename MyIterator::pointer operator->() const;
typename MyIterator::reference operator[](const typename MyIterator::difference_type& n) const;
MyIterator operator++(int);
MyIterator& operator++();
MyIterator operator--(int);
MyIterator& operator--();
MyIterator operator+(const typename MyIterator::difference_type& n) const;
MyIterator& operator+=(const typename MyIterator::difference_type& n);
MyIterator operator-(const typename MyIterator::difference_type& n) const;
MyIterator& operator-=(const typename MyIterator::difference_type& n);
bool operator!=(const MyIterator& it) const;
bool operator==(const MyIterator& it) const;
bool operator<(const MyIterator& it) const;
bool operator>(const MyIterator& it) const;
bool operator<=(const MyIterator& it) const;
bool operator>=(const MyIterator& it) const;
};
And it's methods:
template <typename T>
ArraySequence<T>::MyIterator::MyIterator(T* pos): pos(pos) {}
template <typename T>
ArraySequence<T>::MyIterator::MyIterator(const MyIterator& it): pos(it.pos) {}
template <typename T>
typename ArraySequence<T>::MyIterator::reference ArraySequence<T>::MyIterator::operator*() const {
return *pos;
}
template <typename T>
typename ArraySequence<T>::MyIterator::pointer ArraySequence<T>::MyIterator::operator->() const {
return pos;
}
template <typename T>
typename ArraySequence<T>::MyIterator::reference ArraySequence<T>::MyIterator::operator[](const typename MyIterator::difference_type& n) const {
return *(pos + n);
}
template <typename T>
typename ArraySequence<T>::MyIterator ArraySequence<T>::MyIterator::MyIterator::operator++(int) {
return MyIterator(pos++);
}
template <typename T>
typename ArraySequence<T>::MyIterator& ArraySequence<T>::MyIterator::MyIterator::operator++() {
++pos;
return *this;
}
template <typename T>
typename ArraySequence<T>::MyIterator ArraySequence<T>::MyIterator::MyIterator::operator--(int) {
return MyIterator(pos--);
}
template <typename T>
typename ArraySequence<T>::MyIterator& ArraySequence<T>::MyIterator::MyIterator::operator--() {
--pos;
return *this;
}
template <typename T>
typename ArraySequence<T>::MyIterator ArraySequence<T>::MyIterator::operator+(const typename MyIterator::difference_type& n) const {
return MyIterator(pos + n);
}
template <typename T>
typename ArraySequence<T>::MyIterator& ArraySequence<T>::MyIterator::operator+=(const typename MyIterator::difference_type& n) {
pos += n;
return *this;
}
template <typename T>
typename ArraySequence<T>::MyIterator& ArraySequence<T>::MyIterator::operator-=(const typename MyIterator::difference_type& n) {
pos -= n;
return *this;
}
template <typename T>
typename ArraySequence<T>::MyIterator ArraySequence<T>::MyIterator::operator-(const typename MyIterator::difference_type& n) const {
return MyIterator(pos - n);
}
template <typename T>
bool ArraySequence<T>::MyIterator::operator!=(const MyIterator& it) const {
return pos != it.pos;
}
template <typename T>
bool ArraySequence<T>::MyIterator::operator==(const MyIterator& it) const {
return pos == it.pos;
}
template <typename T>
bool ArraySequence<T>::MyIterator::operator<(const MyIterator& it) const {
return pos < it.pos;
}
template <typename T>
bool ArraySequence<T>::MyIterator::operator>(const MyIterator& it) const {
return pos > it.pos;
}
template <typename T>
bool ArraySequence<T>::MyIterator::operator<=(const MyIterator& it) const {
return pos <= it.pos;
}
template <typename T>
bool ArraySequence<T>::MyIterator::operator>=(const MyIterator& it) const {
return pos >= it.pos;
}
template <typename T>
ArraySequence<T>::MyIterator::~MyIterator() {}
When I try to run std::sort I get a lot of compiler errors such as "Invalid operands to binary expression ('ArraySequence::MyIterator' and 'ArraySequence::MyIterator')" on line "difference_type __len = __last - __first;". What am I missing here? What can be improved?
You forgot that you need to implement operator - for the iterators themselves. Random access iterator support it1 - it2 to get a distance between the two iterators. You have operator - for the difference_type, but not for the iterator type itself. You need to add an overload like:
template <typename T>
typename MyIterator::difference_type ArraySequence<T>::MyIterator::operator-(const typename ArraySequence<T>::MyIterator& n) const
{
return pos - n.pos;
}

operator==() using template template

EDIT: Prolog: I'm a victim of my own ignorance and also of late-night coding.
I'm writing a templated class using template template. It has an iterator, which means that I need to provide an appropriately templated operator==(). This is where I'm having trouble.
Representative code sample follows:
#include <iostream>
#include <typeinfo>
using namespace std;
namespace detail {
template <typename T> class foo {};
template <typename T> class bar {};
}
template <template<class> class A, template<class> class B>
struct basic_thing {
template <typename T> using target_type = A<B<T>>;
target_type<float> fmember;
target_type<int> imember;
struct iterator {
bool equal (const iterator& other) { return true; }
};
iterator begin () { return iterator{}; }
iterator end () { return iterator{}; }
};
template <template<class> class A, template<class> class B>
bool operator== (const typename basic_thing<A, B>::iterator& lhs, const typename basic_thing<A, B>::iterator& rhs) {
return lhs.equal(rhs);
}
int main ()
{
using Thing = basic_thing<detail::foo, detail::bar>;
Thing t;
cout << typeid(t.fmember).name() << endl;
cout << typeid(t.imember).name() << endl;
bool b = (t.begin() == t.end());
return 0;
}
My goal here is to provide a composable way to define basic_thing::target_type, and this pattern works for that purpose. But, I'm stuck at how to declare operator==() for basic_thing::iterator. Either this isn't very straightforward, or there's something obvious that I'm missing. (Likely the latter.)
g++-7.4.0 with -std=c++11 produces the following:
foo.cc: In function 'int main()':
foo.cc:39:23: error: no match for 'operator==' (operand types are 'basic_thing<detail::foo, detail::bar>::iterator' and 'basic_thing<detail::foo, detail::bar>::iterator')
bool b = (t.begin() == t.end());
~~~~~~~~~~^~~~~~~~~~
foo.cc:27:6: note: candidate: template<template<class> class A, template<class> class B> bool operator==(const typename basic_thing<A, B>::iterator&, const typename basic_thing<A, B>::iterator&)
bool operator== (const typename basic_thing<A, B>::iterator& lhs, const typename basic_thing<A, B>::iterator& rhs) {
^~~~~~~~
foo.cc:27:6: note: template argument deduction/substitution failed:
foo.cc:39:32: note: couldn't deduce template parameter 'template<class> class A'
bool b = (t.begin() == t.end());
^
What are some correct ways to do this? Is it even possible when template templates are involved?
The simpler way is to create it inside the struct directly (as member or friend function):
template <template<class> class A, template<class> class B>
struct basic_thing {
// ...
struct iterator {
bool equal (const iterator& other) { return true; }
bool operator ==(const iterator& rhs) const;
// friend bool operator ==(const iterator& lhs, const iterator& rhs);
};
};
With
template <template<class> class A, template<class> class B>
bool operator== (const typename basic_thing<A, B>::iterator& lhs,
const typename basic_thing<A, B>::iterator& rhs);
A and B are not deducible (on the left of ::).
so only callable the ugly way:
bool b = operator==<detail::foo, detail::bar>(t.begin(), t.begin());

Friend function defined inside class template. Function template redefinition error

Below are two code samples. The first one defines operator+() for a class template Vector while the second one just declares the function but moves the function definition outside the class body. The first sample results in the following error:
main.cpp(4): error C2995:
'Vector<L,T> operator +(const Vector<L,T> &,const Vector<L,T> &)' :
function template has already been defined
Here are my questions:
Why do the two template instantiations in main() result in function template redefinition errors? Based on my understanding, they should result in unique instances as the template parameters are different.
How does moving the function definition outside of the class body resolve the error?
Error sample:
template<int L, typename T>
class Vector {
template<int L, typename T> friend
Vector<L, T> operator+(const Vector<L, T>& lhs, const Vector<L, T>& rhs) {
return Vector<L, T>();
}
private:
T data[L];
};
int main() {
Vector<42, double> v42d;
Vector<24, int> v24i;
return 0;
}
Working sample:
template<int L, typename T>
class Vector {
template<int L, typename T> friend
Vector<L, T> operator+(const Vector<L, T>& lhs, const Vector<L, T>& rhs);
private:
T data[L];
};
template<int L, typename T>
Vector<L, T> operator+(const Vector<L, T>& lhs, const Vector<L, T>& rhs) {
return Vector<L, T>();
}
int main() {
Vector<42, double> v42d;
Vector<24, int> v24i;
return 0;
}
The template types L and T are already known, so don't need to be reintroduced. In fact doing so for the friend function causes them to overshadow the ones defined for the class.
this fixes it:
template<int L, typename T>
class Vector {
friend
Vector<L, T> operator+(const Vector<L, T>& lhs, const Vector<L, T>& rhs) {
return Vector<L, T>();
}
private:
T data[L];
};
which is equivalent to:
template<int L, typename T>
class Vector {
friend
Vector operator+(const Vector& lhs, const Vector& rhs) {
return Vector();
}
private:
T data[L];
};

std::optional specialization for reference types

Why std::optional (std::experimental::optional in libc++ at the moment) does not have specialization for reference types (compared with boost::optional)?
I think it would be very useful option.
Is there some object with reference to maybe already existing object semantics in STL?
When n3406 (revision #2 of the proposal) was discussed, some committee members were uncomfortable with optional references. In n3527 (revision #3), the authors decided to make optional references an auxiliary proposal, to increase the chances of getting optional values approved and put into what became C++14. While optional didn't quite make it into C++14 for various other reasons, the committee did not reject optional references and is free to add optional references in the future should someone propose it.
The main problem with std::optional <T&> is — what should optRef = obj do in the following case:
optional<T&> optRef;
…;
T obj {…};
optRef = obj; // <-- here!
Variants:
Always rebind — (&optRef)->~optional(); new (&optRef) optional<T&>(obj).
Assign through — *optRef = obj (UB when !optRef before).
Bind if empty, assign through otherwise — if (optRef) {do1;} else {do2;}.
No assignment operator — compile-time error "trying to use a deleted operator".
Pros of every variant:
Always rebind (chosen by boost::optional and n1878):
Consistency between the cases when !optRef and optRef.has_value() — post-condition &*optRef == &obj is always met.
Consistency with usual optional<T> in the following aspect: for usual optional<T>, if T::operator= is defined to act as destroying and constructing (and some argue that it must be nothing more than optimization for destroying-and-constructing), opt = … de facto acts similarly like (&opt)->~optional(); new (&opt) optional<T&>(obj).
Assign through:
Consistency with pure T& in the following aspect: for pure T&, ref = … assigns through (not rebinds the ref).
Consistency with usual optional<T> in the following aspect: for usual optional<T>, when opt.has_value(), opt = … is required to assign through, not to destroy-and-construct (see template <class U> optional<T>& optional<T>::operator=(U&& v) in n3672 and on cppreference.com).
Consistency with usual optional<T> in the following aspect: both haveoperator= defined at least somehow.
Bind if empty, assign through otherwise — I see no real benefits, IMHO this variant arises only when proponents of #1 argue with proponents of #2, however formally it's even more consistent with the letter of requirements for template <class U> optional<T>& optional<T>::operator=(U&& v) (but not with the spirit, IMHO).
No assignment operator (chosen by n3406):
Consistency with pure T& in the following aspect: pure T& doesn't allow to rebind itself.
No ambiguous behavior.
See also:
Let’s Talk about std::optional<T&> and optional references.
Why Optional References Didn’t Make It In C++17.
There is indeed something that has reference to maybe existing object semantics. It is called a (const) pointer. A plain old non-owning pointer. There are three differences between references and pointers:
Pointers can be null, references can not. This is exactly the difference you want to circumvent with std::optional.
Pointers can be redirected to point to something else. Make it const, and that difference disappears as well.
References need not be dereferenced by -> or *. This is pure syntactic sugar and possible because of 1. And the pointer syntax (dereferencing and convertible to bool) is exactly what std::optional provides for accessing the value and testing its presence.
Update:
optional is a container for values. Like other containers (vector, for example) it is not designed to contain references. If you want an optional reference, use a pointer, or if you indeed need an interface with a similar syntax to std::optional, create a small (and trivial) wrapper for pointers.
Update2: As for the question why there is no such specialization: because the committee simply did opt it out. The rationale might be found somewhere in the papers. It possibly is because they considered pointers to be sufficient.
IMHO it is very okay to make std::optional<T&> available. However there is a subtle issue about templates. Template parameters can become tricky to deal with if there are references.
Just as the way we solved the problem of references in template parameters, we can use a std::reference_wrapper to circumvent the absence of std::optional<T&>. So now it becomes std::optional<std::reference_wrapper<T>>. However I recommend against this use because 1) it is way too verbose to both write the signature (trailing return type saves us a bit) and the use of it (we have to call std::reference_wrapper<T>::get() to get the real reference), and 2) most programmers have already been tortured by pointers so that it is like an instinctive reaction that when they receive a pointer they test first whether it is null so it is not quite much an issue now.
If I would hazard a guess, it would be because of this sentence in the specification of std::experimental::optional. (Section 5.2, p1)
A program that necessitates the instantiation of template optional
for a reference type, or for possibly cv-qualified types in_place_t or
nullopt_t is ill-formed.
I stumbled upon this several times and I finally decided to implement my solution that doesn't depend on boost. For reference types it disables assignment operator and doesn't allow for comparison of pointers or r-values. It is based on a similar work I did some time ago, and it uses nullptr instead of nullopt to signal absence of value. For this reason, the type is called nullable and compilation is disabled for pointer types (they have nullptr anyway). Please let me know if you find any obvious or any non-obvious problem with it.
#ifndef COMMON_NULLABLE_H
#define COMMON_NULLABLE_H
#pragma once
#include <cstddef>
#include <stdexcept>
#include <type_traits>
namespace COMMON_NAMESPACE
{
class bad_nullable_access : public std::runtime_error
{
public:
bad_nullable_access()
: std::runtime_error("nullable object doesn't have a value") { }
};
/**
* Alternative to std::optional that supports reference (but not pointer) types
*/
template <typename T, typename = std::enable_if_t<!std::is_pointer<T>::value>>
class nullable final
{
public:
nullable()
: m_hasValue(false), m_value{ } { }
nullable(T value)
: m_hasValue(true), m_value(std::move(value)) { }
nullable(std::nullptr_t)
: m_hasValue(false), m_value{ } { }
nullable(const nullable& value) = default;
nullable& operator=(const nullable& value) = default;
nullable& operator=(T value)
{
m_hasValue = true;
m_value = std::move(value);
return *this;
}
nullable& operator=(std::nullptr_t)
{
m_hasValue = false;
m_value = { };
return *this;
}
const T& value() const
{
if (!m_hasValue)
throw bad_nullable_access();
return m_value;
}
T& value()
{
if (!m_hasValue)
throw bad_nullable_access();
return m_value;
}
bool has_value() const { return m_hasValue; }
const T* operator->() const { return &m_value; }
T* operator->() { return &m_value; }
const T& operator*() const { return m_value; }
T& operator*() { return m_value; }
public:
template <typename T2>
friend bool operator==(const nullable<T2>& lhs, const nullable<T2>& rhs);
template <typename T2>
friend bool operator!=(const nullable<T2>& lhs, const nullable<T2>& rhs);
template <typename T2>
friend bool operator==(const nullable<std::decay_t<T2>>& lhs, const nullable<T2&>& rhs);
template <typename T2>
friend bool operator!=(const nullable<std::decay_t<T2>>& lhs, const nullable<T2&>& rhs);
template <typename T2>
friend bool operator==(const nullable<T2&>& lhs, const nullable<std::decay_t<T2>>& rhs);
template <typename T2>
friend bool operator!=(const nullable<T2&>& lhs, const nullable<std::decay_t<T2>>& rhs);
template <typename T2>
friend bool operator==(const nullable<T2>& lhs, const T2& rhs);
template <typename T2>
friend bool operator==(const T2& lhs, const nullable<T2>& rhs);
template <typename T2>
friend bool operator==(const nullable<T2>& lhs, std::nullptr_t);
template <typename T2>
friend bool operator!=(const nullable<T2>& lhs, const T2& rhs);
template <typename T2>
friend bool operator!=(const T2& lhs, const nullable<T2>& rhs);
template <typename T2>
friend bool operator==(std::nullptr_t, const nullable<T2>& rhs);
template <typename T2>
friend bool operator!=(const nullable<T2>& lhs, std::nullptr_t);
template <typename T2>
friend bool operator!=(std::nullptr_t, const nullable<T2>& rhs);
private:
bool m_hasValue;
T m_value;
};
// Template spacialization for references
template <typename T>
class nullable<T&> final
{
public:
nullable()
: m_hasValue(false), m_value{ } { }
nullable(T& value)
: m_hasValue(true), m_value(&value) { }
nullable(std::nullptr_t)
: m_hasValue(false), m_value{ } { }
nullable(const nullable& value) = default;
nullable& operator=(const nullable& value) = default;
const T& value() const
{
if (!m_hasValue)
throw bad_nullable_access();
return *m_value;
}
T& value()
{
if (!m_hasValue)
throw bad_nullable_access();
return *m_value;
}
bool has_value() const { return m_hasValue; }
const T* operator->() const { return m_value; }
T* operator->() { return m_value; }
const T& operator*() const { return *m_value; }
T& operator*() { return *m_value; }
public:
template <typename T2>
friend bool operator==(const nullable<std::decay_t<T2>>& lhs, const nullable<T2&>& rhs);
template <typename T2>
friend bool operator!=(const nullable<std::decay_t<T2>>& lhs, const nullable<T2&>& rhs);
template <typename T2>
friend bool operator==(const nullable<T2&>& lhs, const nullable<std::decay_t<T2>>& rhs);
template <typename T2>
friend bool operator!=(const nullable<T2&>& lhs, const nullable<std::decay_t<T2>>& rhs);
template <typename T2>
friend bool operator==(const nullable<T2&>& lhs, const nullable<T2&>& rhs);
template <typename T2>
friend bool operator!=(const nullable<T2&>& lhs, const nullable<T2&>& rhs);
template <typename T2>
friend bool operator==(const nullable<T2&>& lhs, const std::decay_t<T2>& rhs);
template <typename T2>
friend bool operator!=(const nullable<T2&>& lhs, const std::decay_t<T2>& rhs);
template <typename T2>
friend bool operator==(const std::decay_t<T2>& lhs, const nullable<T2&>& rhs);
template <typename T2>
friend bool operator!=(const std::decay_t<T2>& lhs, const nullable<T2&>& rhs);
template <typename T2>
friend bool operator==(const nullable<T2>& lhs, std::nullptr_t);
template <typename T2>
friend bool operator==(std::nullptr_t, const nullable<T2>& rhs);
template <typename T2>
friend bool operator!=(const nullable<T2>& lhs, std::nullptr_t);
template <typename T2>
friend bool operator!=(std::nullptr_t, const nullable<T2>& rhs);
private:
bool m_hasValue;
T* m_value;
};
template <typename T>
using nullableref = nullable<T&>;
template <typename T2>
bool operator==(const nullable<T2>& lhs, const nullable<T2>& rhs)
{
if (lhs.m_hasValue != rhs.m_hasValue)
return false;
if (lhs.m_hasValue)
return lhs.m_value == rhs.m_value;
else
return true;
}
template <typename T2>
bool operator!=(const nullable<T2>& lhs, const nullable<T2>& rhs)
{
if (lhs.m_hasValue != rhs.m_hasValue)
return true;
if (lhs.m_hasValue)
return lhs.m_value != rhs.m_value;
else
return false;
}
template <typename T2>
bool operator==(const nullable<std::decay_t<T2>>& lhs, const nullable<T2&>& rhs)
{
if (lhs.m_hasValue != rhs.m_hasValue)
return true;
if (lhs.m_hasValue)
return lhs.m_value != *rhs.m_value;
else
return false;
}
template <typename T2>
bool operator!=(const nullable<std::decay_t<T2>>& lhs, const nullable<T2&>& rhs)
{
if (lhs.m_hasValue != rhs.m_hasValue)
return true;
if (lhs.m_hasValue)
return lhs.m_value != *rhs.m_value;
else
return false;
}
template <typename T2>
bool operator==(const nullable<T2&>& lhs, const nullable<std::decay_t<T2>>& rhs)
{
if (lhs.m_hasValue != rhs.m_hasValue)
return false;
if (lhs.m_hasValue)
return *lhs.m_value == rhs.m_value;
else
return true;
}
template <typename T2>
bool operator!=(const nullable<T2&>& lhs, const nullable<std::decay_t<T2>>& rhs)
{
if (lhs.m_hasValue != rhs.m_hasValue)
return true;
if (lhs.m_hasValue)
return *lhs.m_value != rhs.m_value;
else
return false;
}
template <typename T2>
bool operator==(const nullable<T2&>& lhs, const nullable<T2&>& rhs)
{
if (lhs.m_hasValue != rhs.m_hasValue)
return false;
if (lhs.m_hasValue)
return *lhs.m_value == *rhs.m_value;
else
return true;
}
template <typename T2>
bool operator!=(const nullable<T2&>& lhs, const nullable<T2&>& rhs)
{
if (lhs.m_hasValue != rhs.m_hasValue)
return true;
if (lhs.m_hasValue)
return *lhs.m_value != *rhs.m_value;
else
return false;
}
template <typename T2>
bool operator==(const nullable<T2&>& lhs, const std::decay_t<T2>& rhs)
{
if (!lhs.m_hasValue)
return false;
return *lhs.m_value == rhs;
}
template <typename T2>
bool operator!=(const nullable<T2&>& lhs, const std::decay_t<T2>& rhs)
{
if (!lhs.m_hasValue)
return true;
return *lhs.m_value != rhs;
}
template <typename T2>
bool operator==(const std::decay_t<T2>& lhs, const nullable<T2&>& rhs)
{
if (!rhs.m_hasValue)
return false;
return lhs == *rhs.m_value;
}
template <typename T2>
bool operator!=(const std::decay_t<T2>& lhs, const nullable<T2&>& rhs)
{
if (!rhs.m_hasValue)
return true;
return lhs != *rhs.m_value;
}
template <typename T2>
bool operator==(const nullable<T2>& lhs, const T2& rhs)
{
if (!lhs.m_hasValue)
return false;
return lhs.m_value == rhs;
}
template <typename T2>
bool operator!=(const nullable<T2>& lhs, const T2& rhs)
{
if (!lhs.m_hasValue)
return true;
return lhs.m_value != rhs;
}
template <typename T2>
bool operator==(const T2& lhs, const nullable<T2>& rhs)
{
if (!rhs.m_hasValue)
return false;
return lhs == rhs.m_value;
}
template <typename T2>
bool operator!=(const T2& lhs, const nullable<T2>& rhs)
{
if (!rhs.m_hasValue)
return true;
return lhs != rhs.m_value;
}
template <typename T2>
bool operator==(const nullable<T2>& lhs, std::nullptr_t)
{
return !lhs.m_hasValue;
}
template <typename T2>
bool operator!=(const nullable<T2>& lhs, std::nullptr_t)
{
return lhs.m_hasValue;
}
template <typename T2>
bool operator==(std::nullptr_t, const nullable<T2>& rhs)
{
return !rhs.m_hasValue;
}
template <typename T2>
bool operator!=(std::nullptr_t, const nullable<T2>& rhs)
{
return rhs.m_hasValue;
}
}
#endif // COMMON_NULLABLE_H