Boost logo

Boost :

Subject: Re: [boost] [type_traits] common_type and SFINAE
From: Matt Calabrese (rivorus_at_[hidden])
Date: 2012-10-04 13:28:06


On Thu, Oct 4, 2012 at 12:19 PM, Lorenzo Caminiti <lorcaminiti_at_[hidden]>wrote:

> On Thu, Oct 4, 2012 at 1:17 AM, Matt Calabrese <rivorus_at_[hidden]> wrote:
> > On Thu, Oct 4, 2012 at 4:13 AM, Matt Calabrese <rivorus_at_[hidden]>
> wrote:
> >
> >> Updated and tested for edge cases int GCC and Clang.
> >>
> >
> > Updated one more time to simplify it a little. I'm going to sleep,
> > otherwise I'll keep changing it.
>
> Thanks a lot!
>
> template< class > struct always_void { typedef void type; };
>
> template< class T, class U, class = void >
> struct common_type_impl_2 {}; // (1)
>
> template< class T, class U >
> struct common_type_impl_2 // (2)
> < T, U
> , typename always_void
> < decltype( true ? std::declval< T >() : std::declval< U >()
> ) >::type // (*)
> >
> {
> typedef decltype( true ? std::declval< T >() : std::declval< U >() )
> type;
> };
>
> So I understand it :) Would you explain me who the 2nd
> common_type_impl2 get selected when there's a common type given that
> always_void<...>::type is always void?
>
> Is it because when decltype( true ? std::declval< T >() :
> std::declval< U >() ) >::type is not valid in the instatiation of
> always_void (*) then SFINAE removes the overload (2) and (1) gets
> selected for being the only choice left? If so, why (1) has the lower
> priority in being selected when there's a common type and both (1) and
> (2) are valid?

When the expression isn't valid, then substitution when trying to match the
specialization fails, meaning it falls back to the default defintiion. When
the expression is valid, the specialization is a better match because the
last parameter (void) is an exact match with the template parameters that
were passed (void was implicitly passed as the default parameter because of
the original template declaration). It's the same way enable_if works only
we don't need a boolean condition since we want the specialization to be
picked for any time the expression is valid, regardless of the result type.

-- 
-Matt Calabrese

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