|
Boost : |
From: Jonathan Turkanis (technews_at_[hidden])
Date: 2004-03-21 04:55:31
Hi,
Often I use enable_if and its cousins to add functionality to code
which works okay on compilers without SFINAE. So I have a lot of code
which looks like this:
template<typename T>
#ifndef BOOST_NO_SFINAE
typename enable_if<is_xxx<T>, int>::type
#else
int
#endif
f(T t);
or
template<typename T>
void f( T t
#ifndef BOOST_NO_SFINAE
, typename enable_if<is_xxx<T>, int>::type* = 0
#endif
);
It would be nice to have macros BOOST_ENABLE_IF_RETURN(condition,
result) and BOOST_ENABLE_IF_PARAM(condition) so that the above could
be written
template<typename T>
BOOST_ENABLE_IF_RETURN(is_xxx<T>, void) f(T);
template<typename T>
void f(T BOOST_ENABLE_IF_PARAM(is_xxx<T>));
(And similarly for enable_if_c, etc.)
It's straightforward to write macros which work for conditions which
don't need to be enclosed in parentheses; e.g.:
#ifndef BOOST_NO_SFINAE
# define BOOST_ENABLE_IF_RETURN(condition, result) \
typename boost::enable_if<condition, result>::type
#else
# define BOOST_ENABLE_IF_RETURN(condition, result) result
#endif
With a little more work, you can allow use such as
template<typename T, typename U>
BOOST_ENABLE_IF_RETURN((is_base_and_derived<T, U>), void)
f(T, U);
e.g.
#define BOOST_ENABLE_IF_RETURN(condition, result) \
typename boost::enable_if< \
typename boost::function_traits< \
void condition \
>::arg1_type, result \
>::type
Here the parentheses in the macro invocation are required. (I've found
I can make the parentheses in the macro invocation optional by
surrounding 'condition' in the above #define with parentheses --
'void(condition)' -- but I'm not sure this is standard conforming. It
does work on a lot of good compilers, though.)
Any thoughts?
Jonathan
Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk