|
Boost : |
Subject: Re: [boost] New, powerful way to use enable_if in C++0x
From: Ryou Ezoe (boostcpp_at_[hidden])
Date: 2011-04-13 08:03:46
On Wed, Apr 13, 2011 at 5:14 PM, Daniel Frey <d.frey_at_[hidden]> wrote:
> On 11.04.2011, at 20:15, Matt Calabrese wrote:
>
>> However, in C++0x, there is a new way to use enable_if by altering neither
>> the function parameter list nor the specification of the return type. It can
>> be used with operators, constructors, variadic function templates, and even
>> overloaded conversion operations (the latter of which was previously
>> considered impossible). The way to do it is with C++0x default function
>> template parameters.
>
> That's really nice, but I think the syntax might be improved. How about adding a ::value to enable_if (and the other templates) to allow your example to look like this:
>
> Â template< class... T, bool = boost::enable_if_c< sizeof...( T ) == 10 >::value >
> Â test( T&&... );
>
> Â template< class T, bool = boost::enable_if< boost::is_arithmetic< T > >::value >
> Â operator T() const;
>
> etc.? Get rid of typename, *&, boost::enabler and if you like, you can write 'bool Enabled = ...' instead of 'bool = ...' to be more verbose in your code. Of course, ::value is non-existent if the condition of enable_if is not met, it is not false. I made a quick test with gcc 4.6.0 and it seems to work fine. Plus it allows extending the existing enable_if without breaking anything (AFAICS).
It does not allow you to overload like this.
template < typename T,
bool = boost::enable_if_c< std::is_pointer<T>::value >::value >
void f( T ) { }
template < typename T,
bool = boost::enable_if_c< std::is_arithmetic<T>::value >::value >
void f( T ) { }
I think ::type * = nullptr-or-0 is enough for this.(works on C++0x,
don't work on C++03)
Once compilers implemented true C++0x which allows null pointer
constant as a non-type template argument.
Until then, enabler is a workaround that works.
But default template-argument for function template is a C++0x feature too.
boost::enabler is not necessary for preventing mistakes.
I think users rarely use explicit template argument speciï¬cation.
-- Ryou Ezoe
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk