问题描述
我正在尝试编写一个类,该类将用容器具有的类型的随机数填充容器:
template<typename random_type>
class rand_helper {
private:
std::mt19937 random_engine;
std::uniform_int_distribution<int> int_dist;
std::uniform_real_distribution<double> real_dist;
public:
rand_helper() = delete;
rand_helper(random_type left_b,random_type right_b);
random_type operator()();
};
template<typename random_type>
rand_helper<random_type>::rand_helper(const random_type left_b,const random_type right_b) :random_engine{ std::random_device{}()} {
if constexpr (std::is_same_v<random_type,double>)
real_dist(left_b,right_b );
else
int_dist( left_b,right_b );
}
template<typename random_type>
random_type rand_helper<random_type>::operator()() {
if constexpr (std::is_same_v<random_type,double>)
return real_dist(random_engine);
else
return int_dist(random_engine);
}
但是这里某处发生了错误,因为当我调用 std::generate 时,我得到了很多错误:
template<typename T,typename vec_type = typename T::value_type>
void fill_contain(T& container,vec_type left_b=vec_type(0),vec_type right_b= vec_type(100)) {
std::generate(std::begin(container),std::end(container),rand_helper<vec_type>(left_b,right_b));
}
我认为可能是因为 if constexpr 但如果只是离开:
template<typename random_type>
random_type rand_helper<random_type>::operator()() {
return int_dist(random_engine);
}
Error C2825 '_Urng': must be a class or namespace when followed by '::'
Error C2510 '_Urng' : left of '::' must be a class / struct / union
Error C2061 Syntax error : identifier 'result_type'
Error C2065 '_Ty1' : undeclared identifier
Error C2923 'std::conditional_t' : '_Ty1' is not a valid template type argument for parameter '_Ty2'
std::vector<int> for_sort;
fill_contain(for_sort);
解决方法
无论 if constexpr
是什么,您的代码都无法编译。仅使用模板类可能不会出现编译错误的原因是,它是一个模板,因此不会编译任何实际实例。如果添加:
template class rand_helper<int>;
强制实例化 random_type
的 int
,您将获得大量编译错误输出。
具体来说,您会被告知您需要一个伪随机生成器来构造一个 uniform_int_distribution<int>
。
不管上述情况 - 您可以使用以下内容:
template <typename T>
using uniform_distribution = std::conditional_t<
std::is_integral_v<T>,std::uniform_int_distribution<T>,std::uniform_real_distribution<T>
>;
只有一个分发成员。在这种情况下,您甚至可能不需要助手类。
,为了避免使用非浮点类型实例化 std::uniform_real_distribution
模板类并获得可能令人困惑的诊断结果,我更愿意使用这样的模板特化而不是 std::conditional_t
:
namespace detail
{
template <typename T,typename AlwaysVoid = void>
struct uniform_distribution_impl
{
static_assert(sizeof(T) == 0,"T must be integral or floating point");
};
template <typename T>
struct uniform_distribution_impl<
T,std::enable_if_t<std::is_integral_v<T>>>
{
using type = std::uniform_int_distribution<T>;
};
template <typename T>
struct uniform_distribution_impl<
T,std::enable_if_t<std::is_floating_point_v<T>>>
{
using type = std::uniform_real_distribution<T>;
};
}
template <typename T>
using uniform_distribution = typename detail::uniform_distribution_impl<T>::type;