Boost logo

Boost :

Subject: Re: [boost] [contract] concept error messages
From: Lorenzo Caminiti (lorcaminiti_at_[hidden])
Date: 2012-10-06 12:03:32


On Fri, Oct 5, 2012 at 5:23 PM, Andrew Sutton <asutton.list_at_[hidden]> wrote:
>> One point of discussion is: In case a type doesn't model a concept,
>> should the compiler generate an hard error or the function/class
>> template should simply be removed from the overload/specialization
>> set?
>
> I think it's best to remove templates from the overload set or
> specialization matching. In other words, check constraints early and
> check constraints often. You should only really get "hard" concept
> errors when you actually end up trying to use a bad overload or
> specialization.

Using concept (boolean meta-functions) with enable_if should be
sufficient then -- just option A):

template< typename T >
typename std::enable_if<
    AlwaysTrue<T>::value && contract::std::EqualityComparable<T>::value,
bool>::type equal1 ( T const& a, T const& b )
{
    return a == b;
}

And given this, I personally don't see the need for specifying
concepts in place of typename or using requires. In other words, I
think the above use of enable_if is sufficient and the following
syntax is not necessary (even if it's part of N3351):

template< AlwaysTrue T >
    requires contract::std::EqualityComparable<T>
bool equal1 ( T const& a, T const& b )
{
    return a == b;
}

What would be the purpose of supporting this syntax given that we can
equivalently use enable_if (which is already part of the standard)?
(I'm only looking at function templates for now, I'm not sure how the
"enable_if" equivalent for class templates will look like and that
might be a reason for using the typename/requires syntax to specify
concepts.)

On some compilers like clang, enable_if might even generate errors
that show the concept signature in a note (even if that's not
guaranteed on all compilers because it is not in general part of the
call failure message):

08.cpp:44:5: error: no matching function for call to 'equal1'
    equal1(abc, abc);
    ^~~~~~
08.cpp:12:5: note: candidate template ignored: disabled by 'enable_if'
[with T = x]
    AlwaysTrue<T>::value && contract::std::EqualityComparable<T>::value,
    ^

Thanks,
--Lorenzo


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