繁体   English   中英

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

[英]Proper use of std::enable_if or how to replace it

这是正确使用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);

BTW与提升我有类似的东西:(我不想使用提升)

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

一旦你像Mr.Anubis所说的那样包装它们,你甚至可以放弃(有时有点神秘)SFINAE hack并使用重载:

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或超载,这是一个品味问题)。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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