Boost logo

Boost :

Subject: Re: [boost] [Fit] Formal review / Kris
From: Paul Fultz II (pfultz2_at_[hidden])
Date: 2016-03-21 14:16:32


On Monday, March 21, 2016 at 8:34:16 AM UTC-5, Louis Dionne wrote:
>
> Paul Fultz II wrote
> > On Sunday, March 20, 2016 at 10:44:45 AM UTC-5, Louis Dionne wrote:
> >>
> >> [...]
> >> How so? The implementations are very similar, except Hana also allows
> >> for non-const function objects. I'd be grateful if you could provide a
> >> benchmark, perhaps I could improve Hana's implementation.
> >>
> >
> > Actually looking at your implementation closer, I realized thats not the
> > case.
> > I originally had an implementation where I ranked the overloads, and
> your
> > implementation looked similar to that when using `which`. However, after
> > looking at it closer, it won't ever instantiate the second function if
> the
> > first function is callable. Sorry for the confusion.
>
> I'm not sure I understand what you mean. I mean, it is expected that
> the function is never called when the first one could be called, since
> it's overload _linearly_, right?
>

Yes, but another way to achieve that is to use a ranked tag-dispatching.
Like
this:

template<int N>
struct rank : rank<N-1>
{};

template<>
struct rank<0>
{};

// Call F
template<class... Ts>
auto foo_impl(Ts&&... xs, rank<1>)
-> decltype(std::declval<F>()(std::forward<Ts>(xs)...));

// Call G
template<class... Ts>
auto foo_impl(Ts&&... xs, rank<0>)
-> decltype(std::declval<G>()(std::forward<Ts>(xs)...));

template<class... Ts>
auto foo_impl(Ts&&... xs)
-> decltype(foo_impl(std::forward<Ts>(xs)..., rank<1>{}));

So `G` will not be called if `F` is callable, however, the compiler will
instantiate both `F` and `G` with this implementation. I used to use an
implementation like this, but it is more costly at compile time.
Furthermore,
lazy instantiation is nice feature as well, especially when using constexpr,
as the body of a constexpr function is always instantiated during
substitution.
 

>
> Louis
>
>
>
>
> --
> View this message in context:
> http://boost.2283326.n4.nabble.com/Fit-Formal-review-Kris-tp4684694p4684766.html
> Sent from the Boost - Dev mailing list archive at Nabble.com.
>
> _______________________________________________
> Unsubscribe & other changes:
> http://lists.boost.org/mailman/listinfo.cgi/boost
>


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk