|
Boost : |
Subject: Re: [boost] [safebool] Can we generalize it and put it into utilities?
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2009-03-30 09:19:49
On Monday, March 30, 2009 7:34 AM
Vladimir Batov wrote:
> > From: "Steven Watanabe" <watanabesj_at_[hidden]>
> > So, here's another take on it:
> >
> > template<class Tag>
> > struct safe_bool {
> > typedef void (safe_bool::*result_type)();
> > result_type operator()(bool b) { return b?
> &safe_bool::true_ : 0 }
> > private:
> > void true_() {}
> > };
>
> Yes, "templatazing" makes sense. The deployment will probably
It isn't immediately obvious why it is sensible, so that must be documented.
> be as follows:
>
> class Foo
> { ...
> operator safe_bool<Foo>::result_type() const { return
> safe_bool<Foo>()(my_condition); }
> };
The extra parentheses to invoke the function call operator will trip up many would-be users, so a named function would be better.
A function template can make this even easier:
template <class Tag>
inline safe_bool<Tag>::result_type
make_safe_bool(bool _value)
{
return safe_bool<Tag>::apply(_value);
}
Usage is now:
class Foo
{
...
operator safe_bool<Foo>::result_type() const
{
return make_safe_bool<Foo>(condition_);
}
};
_____
Rob Stewart robert.stewart_at_[hidden]
Software Engineer, Core Software using std::disclaimer;
Susquehanna International Group, LLP http://www.sig.com
IMPORTANT: The information contained in this email and/or its attachments is confidential. If you are not the intended recipient, please notify the sender immediately by reply and immediately delete this message and all its attachments. Any review, use, reproduction, disclosure or dissemination of this message or any attachment by an unintended recipient is strictly prohibited. Neither this message nor any attachment is intended as or should be construed as an offer, solicitation or recommendation to buy or sell any security or other financial instrument. Neither the sender, his or her employer nor any of their respective affiliates makes any warranties as to the completeness or accuracy of any of the information contained herein or that this message or any of its attachments is free of viruses.
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk