gpt4 book ai didi

c++ - 模板类中的动态模板方法选择

转载 作者:行者123 更新时间:2023-11-30 02:14:50 25 4
gpt4 key购买 nike

我想创建一个模板随机数生成器类,它可以是整数类型或浮点类型。为什么?对于一个作业,我写了一个累加函数(本质上与 std::accumulate 相同),我想制作一个测试工具,它可以是任何整数或浮点类型(例如,unsigned|short|long|long long int、float、双倍的)。我们一直在研究模板,我正在尝试使用模板编程来做出动态的编译时决策。我可能以错误的方式处理这个问题 - 非常感谢任何建议/引用。

这是我的测试函数:

void testdrive() {
std::vector<int> vint(ELEMENTS);
std::vector<double> vflt(ELEMENTS);
RNG<int> intrng;
RNG<double> fltrng;

std::generate(vint.begin(), vint.end(), intrng)
std::generate(vflt.begin(), vflt.end(), fltrng)

std::cout << "Sum of " << printvec(vint) << "is " accum(vint) << "\n\n";
std::cout << "Sum of " << printvec(vflt) << "is " accum(vflt) << '\n';
}

我不知道如何使用我类(class)的模板编程来做到这一点。我想做的是,如果类型是 int 类型,则使用 uniform_int_distribution,如果是 float|double,则使用 uniform_real_distribution。我意识到这两者并不完全可以互换,但对于我正在尝试做的事情来说很好。这是我的类(class):

template<typename T>
class RNG {
public:
RNG(T low=std::numeric_limits<T>::min(),
T high=std::numeric_limits<T>::max())
: rng_engine{rng_seed()}, rng_dist{low, high}, rng_low{low},
rng_high{high} { }
RNG(const RNG& r): rng_engine{rng_seed()}, rng_dist{r.rng_low,
r.rng_high}, rng_low{r.rng_low}, rng_high{r.rng_high} { }
T operator()() { return rng_dist(rng_engine); }
private:
std::random_device rng_seed;
std::mt19937 rng_engine;
template<typename U, typename=std::enable_if_t<std::is_integral<T>::value>>
std::uniform_int_distribution<T> rng_dist;
template<typename U, typename=std::enable_if_t<std::is_floating_point<T>::value>>
std::uniform_real_distribution<T> rng_dist;
T rng_low, rng_high;
};

此外,对于阅读这篇文章的任何人,我发现这本书对于深入研究 C++ 模板非常有帮助:C++ 模板 - 完整指南第 2 版(http://www.tmplbook.com/)

最佳答案

看看template specialization .在下面的代码中,私有(private) struct Distribution 选择要使用的 std::uniform_*_distribution

#include <stdio.h>
#include <vector>
#include <algorithm>
#include <random>
#include <iostream>

template <class T>
class RNG
{
// primary template is designed for integers
template <class U>
struct Distribution
{
typedef std::uniform_int_distribution<U> Type;
};
// explicit specialization for float
template <>
struct Distribution<float>
{
typedef std::uniform_real_distribution<float> Type;
};
// explicit specialization for double
template <>
struct Distribution<double>
{
typedef std::uniform_real_distribution<double> Type;
};

std::random_device rng_source;
typename Distribution<T>::Type rng_dist;

public:
RNG(
T low = std::numeric_limits<T>::min(),
T high = std::numeric_limits<T>::max())
: rng_source{}
, rng_dist(low, high)
{
}
RNG(const RNG& rhs)
: rng_source{}
, rng_dist(rhs.rng_dist)
{
}

T operator()()
{
return rng_dist(rng_source);
}
};

int main()
{
const size_t ELEMENTS = 10;
std::vector<int> vint(ELEMENTS);
std::vector<double> vflt(ELEMENTS);
RNG<int> intrng(0, 100);
RNG<double> fltrng(0.0, 1.0);

std::generate(vint.begin(), vint.end(), intrng);
std::generate(vflt.begin(), vflt.end(), fltrng);

return 0; <-- set a breakpoint here to see both vectors
}

关于c++ - 模板类中的动态模板方法选择,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57128874/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com