如何使用条件来检查类型名 T 是否是 C++ 中浮点类型的整数类型

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

本文关键字:类型 C++ 整数 是否是 条件 何使用 检查      更新时间:2023-10-16

我将编写一个模板来生成随机数据向量。问题是 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

有什么帮助建议吗?

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

正如贾斯汀在他的评论中指出的那样,通过以下方式使用 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
    ...
}

这仅在 17 C++可用。有关编译时类型信息的详细信息,请参阅C++参考:

if constexpr(自C++17起(

<type_traits> (自C++11起(

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

一般常量表达式。

除了布兰登的回答,范围界定是另一个问题。

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

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