Reputation: 2072
I am going to write a template to generate a vector of random data. The problem is
std::uniform_int_distribution
only accepts integer type, and std::uniform_real_distribution
for float type. I want to combine both. Here is my code.
#include <vector>
#include <random>
#include <algorithm>
#include <iterator>
#include <functional>
template<typename T>
std::vector<T> generate_vector(size_t N, T lower = T(0), T higher = T(99)) {
// Specify the engine and distribution.
if constexpr (std::is_integral<T>) {
std::uniform_int_distribution<T> distribution(lower, higher);
}
else if constexpr (std::is_floating_point<T>) {
std::uniform_real_distribution<T> distribution(lower, higher);
}
std::mt19937 engine; // Mersenne twister MT19937
auto generator = std::bind(distribution, engine);
std::vector<T> vec(N);
std::generate(vec.begin(), vec.end(), generator);
return vec;
I am confusing how to implement statements within if conditions. Integer type should include:short, int, long, long long, unsigned short, unsigned int, unsigned long, or unsigned long long
. Float type includes float, double, or long double
.
Any help suggestion?
Upvotes: 14
Views: 14436
Reputation: 460
In addition to Brandon's answer, scoping is another problem.
The following code should suit your needs:
#include <algorithm>
#include <limits>
#include <random>
#include <type_traits>
#include <valarray>
template <class D>
std::valarray<typename D::result_type> generate_valarray(size_t N, D distribution) {
std::random_device device{};
std::mt19937 engine{device()}; // Mersenne twister MT19937
auto generator = std::bind(distribution, engine);
std::valarray<typename D::result_type> container(N);
std::generate(std::begin(container), std::end(container), generator);
return container;
}
template <class T>
std::valarray<T> generate_valarray_uniform(size_t N, const T& lower = std::numeric_limits<T>::min(), const T& higher = std::numeric_limits<T>::max()) {
if constexpr (std::is_integral_v<T>) {
return generate_valarray( N, std::uniform_int_distribution<T>(lower, higher) );
}
else if constexpr (std::is_floating_point_v<T>) {
return generate_valarray( N, std::uniform_real_distribution<T>(lower, higher) );
}
}
#include <iostream>
int main() {
auto f = generate_valarray_uniform(1, 0., 10.)[0];
std::cout << f << std::endl;
}
Upvotes: 0
Reputation: 206667
In a pre-C++17 compiler, you can use template specialization to implement the if
-else
logic.
// Declare a class template
template <bool is_integral, typename T> struct uniform_distribution_selector;
// Specialize for true
template <typename T> struct uniform_distribution_selector<true, T>
{
using type = typename std::uniform_int_distribution<T>;
};
// Specialize for false
template <typename T> struct uniform_distribution_selector<false, T>
{
using type = typename std::uniform_real_distribution<T>;
};
template<typename T>
std::vector<T> generate_vector(size_t N, T lower = T(0), T higher = T(99))
{
// Select the appropriate distribution type.
using uniform_distribution_type = typename uniform_distribution_selector<std::is_integral<T>::value, T>::type;
uniform_distribution_type distribution(lower, higher);
std::mt19937 engine;
auto generator = std::bind(distribution, engine);
std::vector<T> vec(N);
std::generate(vec.begin(), vec.end(), generator);
return vec;
}
Upvotes: 12
Reputation: 908
As Justin points out in his comment it is simple enough to use an if constexpr
block in the following way:
#include <type_traits>
if constexpr (std::is_integral_v<T>) { // constexpr only necessary on first statement
...
} else if (std::is_floating_point_v<T>) { // automatically constexpr
...
}
This is only available C++17. See the C++ references for more information on compile-time type information:
if constexpr
(since C++17)
<type_traits>
(since C++11)
constexpr
specifier (since C++11)
Constant Expressions in general.
Upvotes: 14