|
Boost : |
From: Jorge Lodos (lodos_at_[hidden])
Date: 2006-04-17 15:47:40
Hi
It would be nice if operator== could be added to boost::any. A partial
solution would be the following:
template<typename ValueType>
bool operator==(const ValueType & rhs) const
{
return content ? content->equal(any(rhs).content) :
content == any(rhs).content;
}
bool operator==(const any &rhs) const
{
return content && rhs.content ?
content->equal(rhs.content) :
content == rhs.content;
}
equal is a pure virtual member of placeholder declared as:
virtual bool equal(const placeholder *rhs) const = 0;
and implemented as (in holder):
virtual bool equal(const placeholder *rhs) const
{
return type() == rhs->type() &&
held == static_cast<const holder<ValueType>*>(rhs)->held;
}
However, this will break existing code when the holded object class does no
have operator== defined.
Kevlin Henney proposed:
namespace equality
{
template<typename Type>
bool operator==(const Type &lhs, const Type &rhs)
{
return &lhs == &rhs;
}
}
virtual bool equal(const placeholder *rhs) const
{
using namespace equality;
return type() == rhs->type() &&
held == static_cast<const holder<ValueType>*>(rhs)->held;
}
Thus providing a default operator== for everyone not having its own, which
returns true if the object addresses are the same. This will prevent
compiler errors when the operator== really needed to be there.
What I think is needed is:
1. If operator== is not used there should be no need for holded classes to
implement it.
2. If operator== is used, and the holded class does not define it, the code
must not compile.
Do you think this is possible to implement?
Thanks in advance
Jorge Lodos
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk