Boost logo

Boost :

From: Michael Marcin (mmarcin_at_[hidden])
Date: 2007-04-19 17:10:31


I want to enable a function templated on a type T if a function it relies on
overloaded on that type.

All I know is that this compiles for types I want to enable on and fails to
compile for types I want to disable on.

static float (*const func ) ( float , float ) = std::atan2; // compiles
static int (*const func ) ( int , int ) = std::atan2; // fails to compile

Ideally I think I'd like to do something like:

template< typename T >
typename enable_if< is_overloaded< T ( T ), std::asin > >::type
some_function( T x )
{
    std::cout << std::asin(x);
}

I'm not even sure if I really need this or not.

The best I've come up with has problems with the compiler crashing in some
cases.
It is illustrated by the following toy example (only tested with vs2005).

namespace method
{
    namespace detail
    {
        template< typename Signature, Signature* signature, typename T =
void >
        struct enable_if_overloaded
        {
            typedef T type;
        };
    } // end namespace detail

    template< typename T > inline
    typename detail::enable_if_overloaded<T (T), std::asin, radians<T>
>::type
    my_asin( T x )
    {
        return radians<T>( std::asin( x ) );
    }

    template< typename T > inline
    typename boost::enable_if< boost::is_integral<T>, radians<float> >::type
    my_asin( T x )
    {
        return my_asin( static_cast<float>(x) );
    }

} // end namespace method

void test()
{
    method::my_asin( 1.1f );
    method::my_asin( 1 );
}

Thanks,

Michael Marcin


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