如何使用条件来检查类型名 T 是否是 C++ 中浮点类型的整数类型
How to use condition to check if typename T is integer type of float type in C++
我将编写一个模板来生成随机数据向量。问题是 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;
}
相关文章:
- ArduinoJson 6.15.2:JsonObject没有命名类型
- 防止主数据类型C++的隐式转换
- 大量序列中核苷酸类型的快速计数
- 如何从C++中的依赖类型中获得它所依赖的类型
- 有关插入适配器的错误。[错误]请求从 'back_insert_iterator<vector<>>' 类型转换为非标量类型
- 是否可以初始化不可复制类型的成员变量(或基类)
- 如何获取std::result_of函数的返回类型
- 从父命名空间重载类型
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗
- 我想将一个对T类型的非常量左值引用绑定到一个T类型的临时值
- Openssl 1.1.1d无效使用不完整的类型"struct dsa_st"
- 访问者访问变体并返回不同类型时出错
- 在VS2010-VS2015下编译时,如何使用decltype作为较大类型表达式的LHS
- 处理小于cpu数据总线的数据类型.(c++转换为机器代码)
- C++ 雷神库 - 使用资源加载器类时出现问题(不命名类型)
- 模板元程序查找相似的连续类型名称
- 是否可以从int转换为enum类类型
- 构造函数正在调用一个使用当前类类型的函数
- 我应该使用什么来代替void作为变体中的替代类型之一
- 类中的字符串不命名类型