Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/cplusplus/137.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/elixir/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C++ 我如何正确地实施;运算符();加上;如果constexpr";所以它可以与std::generate一起工作?_C++_Random_Template Instantiation - Fatal编程技术网

C++ 我如何正确地实施;运算符();加上;如果constexpr";所以它可以与std::generate一起工作?

C++ 我如何正确地实施;运算符();加上;如果constexpr";所以它可以与std::generate一起工作?,c++,random,template-instantiation,C++,Random,Template Instantiation,我试图编写一个类,该类将使用容器具有的类型的随机数填充容器: 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; publi

我试图编写一个类,该类将使用容器具有的类型的随机数填充容器:

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::vector<int> for_sort;
    fill_contain(for_sort);

std::用于_排序的向量;
填充包含(用于排序);

您的代码就是无法编译,不管
if constexpr
。仅使用template类可能不会出现编译错误的原因是,它是一个模板,因此不会编译任何实际实例。如果您添加:

template class rand_helper<int>;

只有一个分发成员。在这种情况下,您甚至可能不需要helper类。

为了避免用非浮点类型实例化
std::uniform\u real\u distribution
模板类并得到潜在的混淆诊断,我宁愿使用这样的模板专门化,而不是
std::conditional\u t

名称空间详细信息
{
模板
结构均匀分布
{
静态断言(sizeof(T)=0,“T必须是整数或浮点”);
};
模板
结构均匀分布<
T、 std::如果>
{
使用type=std::均匀分布;
};
模板
结构均匀分布<
T、 std::如果>
{
使用type=std::均匀实分布;
};
}
模板
使用统一分布=typename detail::统一分布\u impl::type;

重新打开:这个问题显然不是问如何在编译时生成随机数,而且没有一个答案可以解释这个编译器错误。@chris哦,你说得对,我误读了这个问题。这是一个粗心的闭包,感谢您捕捉到它。也就是说,您可以将std::conditional与
std::uniform\u int\u distribution
std::uniform\u real\u distribution
一起使用?就我所记得的,它从几个元素中输出一个通用类型,在这里它也可以从这些元素中完成?@Alpharius:是的。我甚至无法想象它应该如何工作。我认为可以通过模板选择一个类型。看看你所说的示例会非常有趣about@Alpharius:见编辑。非常感谢您,我甚至没有想过。也就是说,在你看来,使用
均匀分布
并在lambda函数的某个地方定义一个随机生成器(例如)比生成函子更好?这是一个很好的补充!
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);

template class rand_helper<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>
>;