relaxxx
relaxxx

Reputation: 7824

Proper use of std::enable_if or how to replace it

Is this proper use of std::enable_if? It works, but is it correct?

//*.h file

template <typename T>
static typename std::enable_if<std::is_integral<T>::value, T>::type 
randomFrom(const T min, const T max);


template <typename T>
static typename std::enable_if<std::is_floating_point<T>::value, T>::type 
randomFrom(const T min, const T max);

.

//*.inl file

template <typename T>
inline typename std::enable_if<std::is_integral<T>::value, T>::type 
Math::randomFrom(const T min, const T max)
{
    static std::default_random_engine re((unsigned long)time(0));
    std::uniform_int_distribution<T> uni(min, max);
    return static_cast<T>(uni(re));
}

template <typename T>
inline typename std::enable_if<std::is_floating_point<T>::value, T>::type 
Math::randomFrom(const T min, const T max)
{
    static std::default_random_engine re((unsigned long)time(0));
    std::uniform_real_distribution<T> uni(min, max);
    return static_cast<T>(uni(re));
}

How can I rewrite it, to achieve cleaner interface? Like:

template <typename T>
static T randomFrom(const T min, const T max);

BTW with boost I had something like: (i do not want to use boost)

typedef typename boost::mpl::if_<
        boost::is_floating_point<T>,
        boost::uniform_real<>,
        boost::uniform_int<>>::type dist_type;

and whole behavior was solved in single function. But there is nothing like std::if right?

Upvotes: 4

Views: 1215

Answers (3)

Christian Rau
Christian Rau

Reputation: 45948

Once you wrap them like Mr.Anubis also suggests, you can even drop the (sometimes a bit cryptic) SFINAE hack and use overloading instead:

namespace detail
{
    template <typename T>
    T randomFromImpl(const T min, const T max, const std::true_type&)
    {
        //integer implementation
    }

    template <typename T>
    T randomFromImpl(const T min, const T max, const std::false_type&)
    {
        //float implementation
    }
}

template <typename T>
T randomFrom(const T min, const T max)
{
    static_assert(std::is_arithmetic<T>::value, "unsupported type");
    return detail::randomFromImpl(min, max, std::is_integral<T>());
}

Other than that, your use of std::enable_if is indeed correct, even if not necessarily needed (but I guess it's a matter of taste if you prefer SFINAE or overloading).

Upvotes: 3

Mr.Anubis
Mr.Anubis

Reputation: 5322

Simply wrap them I guess?

template <typename T>
inline typename std::enable_if<std::is_integral<T>::value, T>::type 
randomFrom_helper(const T min, const T max)
{
    static std::default_random_engine re((unsigned long)time(0));
    std::uniform_int_distribution<T> uni(min, max);
    return static_cast<T>(uni(re));
}

template <typename T>
inline typename std::enable_if<std::is_floating_point<T>::value, T>::type 
randomFrom_helper(const T min, const T max)
{
    static std::default_random_engine re((unsigned long)time(0));
    std::uniform_real_distribution<T> uni(min, max);
    return static_cast<T>(uni(re));
}


template <typename T>
T randomFrom(const T min, const T max)
{
return randomFrom_helper(min,max);
}

Upvotes: 4

Luc Danton
Luc Danton

Reputation: 35449

Your usage is fine, and very much idiomatic.

The equivalent to Boost.MPL's if_ would be std::conditional:

typedef typename std::conditional<
        std::is_floating_point<T>::value,
        std::uniform_real_distribution<T>,
        std::uniform_int_distribution<T>>::type dist_type;

Upvotes: 8

Related Questions