Home How to use condition to check if typename T is integer type of float type in C++
Reply: 2

How to use condition to check if typename T is integer type of float type in C++

Dan Jiadong
1#
Dan Jiadong Published in 2018-01-11 04:36:24Z

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?

R Sahu
2#
R Sahu Reply to 2018-01-11 05:31:23Z

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;
}
Brandon Gomes
3#
Brandon Gomes Reply to 2018-01-11 04:59:59Z

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.

You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.306887 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO