Boost logo

Boost :

Subject: Re: [boost] review request: addition to type_traits library of has_operator_xxx
From: Stewart, Robert (Robert.Stewart_at_[hidden])
Date: 2011-01-19 12:18:33


[This was stuck in my drafts folder unbeknownst to be.]

Jeffrey Lee Hellrung, Jr. wrote:
> On 11/17/2010 12:26 PM, Stewart, Robert wrote:
> > Jeffrey Lee Hellrung, Jr. wrote:
> >> On 11/17/2010 6:58 AM, Stewart, Robert wrote:
> >>> Jeffrey Lee Hellrung, Jr. wrote:
> >>>>
> >>> Perhaps checking for convertible-to-return-type could be
> >>> the default predicate rather than adding another template
> >>> parameter?
> >>
> >> How do you envision the interface then? Are you suggesting
> >> that the convertible-to-return-type type and the Boost.MPL
> >> metapredicate occupy the same template parameter?
> >
> > I'm thinking something like this:
> >
> > template
> > <
> > class T
> > , class U = T
> > , class Pred = boost::type_traits::result_converts_to<void>
> >>
> > struct is_less_than_comparable;
> >
> >> It seems to me that, by far, the common case is checking
> >> convertibility to some given type, so I think it makes
> >> sense to make the common case syntactically simple.
> >
> > is_less_than_comparable<int, int, bool> would become
> > is_less_than_comparable<int, int, result_converts_to<bool> >
> > which is more verbose but also more obvious and indicative
> > of the additional power.
>
> Okay. I don't like the verbosity but it's a minor issue.
>
> >> If you want to avoid the extra template parameter, maybe
> >> you can wrap the predicate in something which could be
> >> detected in the Result template parameter, e.g.,
> >
> > That would be ideal, but I'm not sure there's a satisfying
> > means to do so without imposing too much on the acceptable
> > predicates.
>
> The mechanism I gave doesn't impose anything on the predicate
> (as far as I can see). It does exclude return types that are
> instances of the wrapper, but I think that is acceptable.

It seems I scanned past your suggestion without understanding the purpose, but include it here for further discussion:

> >> template< class P > struct operator_predicate;
> >>
> >> and then specialize is_less_than_comparable for that wrapper,
> >> so that the whole thing is
> >>
> >> template< class T0, class T1 = T0, class Result = void >
> >> struct is_less_than_comparable { ... };
> >>
> >> template< class T0, class T1, class P >
> >> struct is_less_than_comparable< T0, T1, operator_predicate<P> >
> >> { ... };

Your idea is to require users of is_less_than_comparable to wrap a predicate with operator_predicate, right? That leads to:

   is_less_than_comparable
   <
      int
      , int
>

   is_less_than_comparable
   <
      int
      , int
      bool
>

   is_less_than_comparable
   <
      int
      , int
      , operator_predicate<my_predicate_type>
>

That means the most common use case is easiest, the next most common use case is as convenient as it can be, and the least common use case is more verbose, but hardly onerous. I like it.

The next question is whether the extra template parameter alternative is better:

   is_less_than_comparable
   <
      int
      , int
      , void
      , my_predicate_type
>

While anyone smart enough to create an MPL predicate for the purpose can deal with the extra template parameter, I find that really odd. I prefer your operator_predicate idea, despite the slightly greater verbosity.

Here's another idea:

   template <class Pred, class>
   struct use_predicate
   { ... };

   template <class From, class To>
   struct convertible
   { ... };

   template
   <
      class T
      , class U
      , class V = void
      , template <class, class> P = convertible
>
   struct is_less_than_comparable
   { ... };

If the convertibility check is done in convertible, then it needs two template parameters: the return type of the operator in question and V. For compatibility, use_predicate would also take two template parameters, but possibly wouldn't use the second. Thus, P<V, return-type-of-operator> is evaluated to yield is_less_than_comparable's result.

(If the convertibility check is done in is_less_than_comparable, then convertible and use_predicate only need one template parameter.)

Now the examples become:

   is_less_than_comparable
   <
      int
      , int
>

   is_less_than_comparable
   <
      int
      , int
      bool
>

   is_less_than_comparable
   <
      int
      , int
      , my_predicate_type
      , use_predicate
>

Obviously, convertible_to<W, void> always yields true/true_type.

That looks cleaner (than the extra template parameter version, at least) and doable, though I haven't tried it.

_____
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