I was trying to write a class that will fill a container with random numbers with the type that the container has:
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); } But here an error occurs somewhere, because when I call std::generate,then I get a lot of errors:
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)); } I thought it might be because of if constexpr but if just leave:
template<typename random_type> random_type rand_helper<random_type>::operator()() { return int_dist(random_engine); } then the same errors are still returned. Here is the list of errors I get:
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' The function call goes like this:
std::vector<int> for_sort; fill_contain(for_sort);