std::enable_if的正确使用或如何替换它

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

本文关键字:何替换 替换 if enable std      更新时间:2023-10-16

这是否正确使用std::enable_if?它有效,但正确吗?

//*.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));
}

我如何重写它,以实现更干净的界面?类似:

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

顺便说一句,我得到了一些类似的东西:(我不想使用助推)

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

并在单个函数中求解整个行为。但没有什么比std::if更好的了,对吧?

您的用法很好,而且非常地道。

与Boost.MPL的if_等效的是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;

我想只是简单地包装它们?

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

一旦你像Anubis先生建议的那样包装它们,你甚至可以放弃(有时有点神秘)SFINAE破解,转而使用重载:

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

除此之外,你对std::enable_if的使用确实是正确的,即使不一定需要(但我想这是一个品味问题,如果你喜欢SFINAE或过载)。