如何使用condition来检查typename T是否是C ++中float类型的整数类型

时间:2018-01-11 04:36:24

标签: c++ templates

我打算写一个模板来生成一个随机数据向量。问题是 std::uniform_int_distribution只接受整数类型,std::uniform_real_distribution表示浮点类型。我想两者结合起来。这是我的代码。

#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;

我很困惑如何在if条件下实现语句。整数类型应包括:short, int, long, long long, unsigned short, unsigned int, unsigned long, or unsigned long long。浮点类型包括float, double, or long double

有任何帮助建议吗?

3 个答案:

答案 0 :(得分:7)

在C ++ 17之前的编译器中,您可以使用模板特化来实现if - else逻辑。

// 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;
}

答案 1 :(得分:4)

Justin在his comment中指出,以下列方式使用if constexpr块很简单:

#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
    ...
}

这只适用于C ++ 17。有关编译时类型信息的更多信息,请参阅C ++参考:

if constexpr(自C ++ 17起)

<type_traits>(自C ++ 11起)

constexpr说明符(自C ++ 11起)

Constant Expressions一般。

答案 2 :(得分:0)

除了Brandon's answer,还有scoping is another problem

以下代码应满足您的需求:

#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;
}