|
Boost : |
Subject: Re: [boost] Is there BOOST_ENABLE_IF macro now?
From: paul Fultz (pfultz2_at_[hidden])
Date: 2013-08-18 16:16:33
> From: TONGARI J
>Hi folks,
>
>It's over 2 years since this post:
>http://boost.2283326.n4.nabble.com/New-powerful-way-to-use-enable-if-in-C-0x-td3442723.html
>
>The macro looks sweet and I wonder if we have this macro in Boost in case
>that I missed it.
>
This is nice for C++11 and conversion operators, I was not aware of this
trick. However, it would be nice if boost provided requires macros to make it
easier to specify traits for enable_if. See here for an implementation:
https://github.com/pfultz2/Zen/blob/master/zen/requires.h
So functions can be defined like this:
template<class T, class U>
ZEN_FUNCTION_REQUIRES(is_arithmetic<T>, is_arithmetic<U>)
(T) max(T x, U y)
{
return x > y : x ? y;
}
All the traits our inclusive by default, but the `exclude` keyword can be
used to exclude the trait, like this:
template<class T, class U>
ZEN_FUNCTION_REQUIRES
(
is_arithmetic<T>,
is_arithmetic<U>,
exclude is_same<T, bool>,
exclude is_same<U, bool>
)
(T) max(T x, U y)
{
return x > y : x ? y;
}
It can be used in classes like this:
template<class T, ZEN_REQUIRES(is_integral<T>)>
class A { ... };
Or this:
template <class T, class Enable = void>
class A { ... };
template <class T>
class A<T, ZEN_CLASS_REQUIRES(is_integral<T>)> { ... };
template <class T>
class A<T, ZEN_CLASS_REQUIRES(is_float<T>)> { ... };
This will all work on C++03 compilers as well. It could also be extended
fairly easily to support the trick from Matt Calabrese. Plus, the
`FUNCTION_REQUIRES` seems like it could be easily converted to use the
`requires` and concept overloading when it gets added to the language as well.
What do you think?
Thanks,
Paul Fultz II
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk