【问题标题】:How to use condition to check if typename T is integer type of float type in C++如何使用条件检查类型名 T 是否为 C++ 中浮点类型的整数类型
【发布时间】:2018-01-11 04:36:24
【问题描述】:

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


【解决方案1】:

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

【讨论】:

    【解决方案2】:

    正如贾斯汀在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 起)

    &lt;type_traits&gt; (C++11 起)

    constexpr 说明符(C++11 起)

    Constant Expressions 一般。

    【讨论】:

    【解决方案3】:

    除了Brandon's answerscoping 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;
    }
    

    【讨论】:

      猜你喜欢
      • 2015-12-24
      • 1970-01-01
      • 1970-01-01
      • 2016-05-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-05-03
      • 1970-01-01
      相关资源
      最近更新 更多