|
Boost : |
From: Daryle Walker (darylew_at_[hidden])
Date: 2003-11-28 21:06:37
On 11/27/03 6:09 PM, "Joel de Guzman" <joel_at_[hidden]> wrote:
> David Abrahams <dave_at_[hidden]> wrote:
>> Daryle Walker <darylew_at_[hidden]> writes:
>>
>>> All of the operator helpers need the final type to provide a core routine.
>>> AFAIK, Peter's idiom is to use a pointer-to-member(-function) as the core
>>> Boolean routine; it can't be provided by a library.
>>
>> What am I missing?
That you two (Dave and Joel) are trying to break the letter of my message,
but are still conforming to its spirit.
Dave helped write the operator-helpers library, so he should know what I'm
talking about. I guess my wording isn't accurate.
All the operator helpers provide some extra operators. These extra
operators are defined in terms of other operators. These "other" operators
are the "core routines" I am referring to.
For example, the helper for "operator /" needs the author of the final class
to provide an "operator /=". Here, "operator /" is the extra operator and
"operator /=" is the core routine. Helper classes can't be used for all the
operators, because the core routines that the operators call must come from
the author.
>> template <class T>
>> struct bool_testable
>> {
>> private:
>> struct impl { int x; };
>> typedef int impl::*safe_bool;
>> public:
>> operator safe_bool() const
>> {
>> return !*static_cast<T const*>(this) ? 0 : &impl::x;
>> }
>> };
As far as "Peter's idiom" is concerned, I meant that the author has to
provide the "operator SomeBuiltInSingularType" for the core Boolean
conversion routine, and a helper class can provide the related operators
like "operator !". All you did was switch the core and derived routines!
Either way, one Boolean-related routine has to be provided by the author.
>> struct X : bool_testable<X>
>> {
>> bool operator!() const { return true; }
>> };
>>
>>
>> struct Y : bool_testable<Y>
>> {
>> bool operator!() const { return false; }
>> };
>>
>>
>> int main()
>> {
>> X x;
>> Y y;
>> if (x) { return 1; }
>>
>> if (y)
>> return 0;
>>
>> // if (x == true) return 1; // fails to compile
>> // if (x == y) return 1; // fails to compile
>>
>> return 0;
>> }
>
> Indeed. A library solution is possible and better. The only thing
> I do not like with the solution above is the inheritance, which implies
> multi-inheritance when X or Y are already subclasses. Well, you can
> chain the inheritance, but... The solution that I posted a while ago
> doesn't require any inheritance at all.
-- Daryle Walker Mac, Internet, and Video Game Junkie darylew AT hotmail DOT com
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk