简体   繁体   中英

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?

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;

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);
}

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).

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM