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);
顺便说一句,我得到了一些类似的东西:(我不想使用助推)
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或过载)。
相关文章:
- 模板参数替换失败,并且未完成隐式转换
- 如何用转义符替换字符串中的所有特殊字符
- 为什么除非添加括号,否则构造函数上的模板替换会失败?
- 在一个读写器队列中,我可以用volatile替换原子吗
- 用符号版本替换对函数的所有调用
- 如何通过替换顺序代码的while循环来添加OpenMP for循环
- 替换基于地图的所有引用
- 按平均值替换数组中的元素
- 我可以在这里替换什么,因为我不能在 C# 中使用隐式变量的 lambda 函数?
- 如何将字节数组元素替换为修改的十六进制 ASCII 符号?
- 初始化 std::vector 替换为单大括号而不是双大括号
- 删除/替换C++字符串中的多字符 (ÿû)
- 将 malloc 替换为数组
- 如何替换此示例代码片段中已弃用的handler_type_t或 boost::asio::handler_type?
- 如何在C++中用'\'替换''来处理转义序列?
- 替换密码:哪一个?
- 替换 C++17 中移除的绑定 1st
- 将 Boost MPL 容器替换为 C++17 功能
- C++:如何用单个命令替换复杂的迭代?
- C++ 替换字节数组中项的顺序