Boost logo

Boost :

From: David Abrahams (dave_at_[hidden])
Date: 2003-07-09 12:43:10


Jaakko Jarvi <jajarvi_at_[hidden]> writes:

> Dear Boosters,
>
> The enable_if library defines the enable_if and disable_if templates,
> which are tools for controlling which function templates are included
> in the overload resolution set based on the properties of the argument
> types.
>
> The following example demonstrates their use:
>
> template <class T>
> typename enable_if<boost::is_arithmetic<T>::value, void>::type foo(T t) {
> std::cout << "An arithmetic type\n";
> }
>
> template <class T>
> typename disable_if<boost::is_arithmetic<T>::value, void>::type foo(T t) {
> std::cout << "Not an arithmetic type\n";
> }
>

I strongly prefer this interface:

    // enable_if operates on types with a nested ::value
    template <class T>
    typename enable_if<boost::is_arithmetic<T>, void>::type foo(T t) {
      std::cout << "An arithmetic type\n";
    }

    template <class T>
    typename disable_if<boost::is_arithmetic<T>, void>::type foo(T t) {
      std::cout << "Not an arithmetic type\n";
    }

    // and enable_if_c operates on integral constants:

    template <class T>
    typename enable_if_c<boost::is_arithmetic<T>::value, void>::type bar(T t) {
      std::cout << "An arithmetic type\n";
    }

    template <class T>
    typename disable_if_c<boost::is_arithmetic<T>::value, void>::type bar(T t) {
      std::cout << "Not an arithmetic type\n";
    }

For the sake of MPL compatibility. It's also nearly always a terser
and more portable usage.

> The directory structure in the tar-ball (or zip-ball) does not fully
> reflect boost directory structure. The library is intended to be part
> of Boost.Utility, and be included as part of including
> "boost/utility.hpp".
>
> There's no Jamfile yet, but there is a Makefile that compiles and runs
> all the tests. Note that GCC 3.2 fails on the test:
>
> enable_if_no_disambiguation.cpp

Please see the test code and #define (BOOST_NO_SFINAE) in
boost/iterator/detail/config_def.hpp and the workaround code in
boost/iterator/detail/enable_if.hpp

Regards,
Dave

-- 
Dave Abrahams
Boost Consulting
www.boost-consulting.com

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